Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print values of void pointer

I have a funciton that returns a void pointer. Lets say I know that the block of data pointed at is an array of ints. How can I print them?

From another thread I saw that I cast the void as my desired data type this way:

printf("%i",*((int*)data));

But like I said that data is an array of ints. I tried to do this, but it's not a valid expression:

for(i = 0; i<3; i++){
    printf("%i \n", *((int*)(data+sizeof(int)*i)));
}

What is the proper way of printing this?

like image 716
Ann Avatar asked Jun 05 '13 21:06

Ann


People also ask

What is the value of void pointer?

The null pointer is basically used in a program to assign the value 0 to a pointer variable of any data type. The void pointer, on the other hand, has no value assigned to it and we use it to store the addresses of other variables in the program- irrespective of their data types.

Does void * have size in C?

The size of void pointer varies system to system. If the system is 16-bit, size of void pointer is 2 bytes. If the system is 32-bit, size of void pointer is 4 bytes. If the system is 64-bit, size of void pointer is 8 bytes.

What should a void pointer return?

The malloc() and calloc() function return the void pointer, so these functions can be used to allocate the memory of any data type.

Can we get type ID of void pointer?

Call one of the two overloads of the GetPointer function to retrieve the void pointer of the Object converted / casted or not. If you just want the reference to the object that the void pointer of the Object points at, then just call the GetReference function instead GetPointer .


2 Answers

Just use a temporary int*:

int *p = data;
for (int i=0; i<3; i++)
    printf("%i\n", p[i]);

Note that there's no explicit cast in the first line, because that's not needed with void* and avoiding explicit casts is considered good C style. Inside the loop, I've used the fact that you can index into a pointer to an array as if it were the array itself.

like image 161
Fred Foo Avatar answered Sep 20 '22 09:09

Fred Foo


Your original attempt is incorrect in "formal" C, since pointer arithmetic is not applicable to void * pointers. However, some compilers support pointer arithmetic on void * pointers as a non-standard extension (GCC being a notable example). In such compilers void * pointers are treated as char * pointers for arithmetic purposes. And your

for(i = 0; i < 3; i++)
    printf("%i\n", *((int *) (data + sizeof(int) * i)));

will actually work as intended in such compiler.

With a more pedantic compiler, you can save the day by doing

for(i = 0; i < 3; i++)
    printf("%i\n", *((int *) ((char *) data + sizeof(int) * i)));

But in any case, the above two pieces of code are hopelessly overcomplicated. They have some value as examples when you are just learning pointer arithmetic, but that's about all they are good for.

What you really need is just

for(i = 0; i < 3; i++)
    printf("%i\n", ((int *) data)[i]);

You can introduce an extra int * pointer, as other answers suggested, to make your code even more readable. But that's up to you.

like image 29
AnT Avatar answered Sep 18 '22 09:09

AnT