views:

8358

answers:

7

Is it possible to dereference the void pointer without type-casting in C programming language?

Also, is there is any way of generalizing a function which can receive a pointer and store it in void pointer and by using that void pointer we can make a generalized function?

for e.g.

void abc(void *a, int b)
{
   if(b==1)
      printf("%d",*(int*)a);     // If integer pointer is received
   else if(b==2)
      printf("%c",*(char*)a);     // If character pointer is received
   else if(b==3)
      printf("%f",*(float*)a);     // If float pointer is received
}

I want to make this function generic, without using ifs; is it possible?

Also if there are some internet articles which explain the concept of void pointer, then it would be beneficial if you could provide the URLs.

Also, is pointer arithmetic with void pointers possible?

+16  A: 

Is it possible to dereference the void pointer without type-casting in C programming language...

No, void indicates the absence of type, it is not something you can dereference or assign to.

is there is any way of generalizing a function which can receive pointer and store it in void pointer and by using that void pointer we can make a generalized function..

You cannot just dereference it in a portable way, as it may not be properly aligned. It may be an issue on some architectures like ARM, where pointer to a data type must be aligned at boundary of the size of data type (e.g. pointer to 32-bit integer must be aligned at 4-byte boundary to be dereferenced).

For example, reading uint16_t from void*:

/* may receive wrong value if ptr is not 2-byte aligned */
uint16_t value = *(uint16_t*)ptr;
/* portable way of reading a little-endian value */
uint16_t value = *(uint8_t*)ptr
                | ((*(uint8_t*)(ptr+1))<<8);

Also, is pointer arithmetic in case void pointers possible...

Pointer arithmetic is not possible on pointers of void due to lack of concrete value underneath the pointer and hence the size.

void* p = ...
void *p2 = p + 1; /* what exactly is the size of void?? */
Alex B
Unless I recall incorrectly, you can safely dereference a void* in two ways. Casting to char* is always acceptable, and if you know the original type it points to you can cast to that type. The void* has lost the type info so it'd have to be stored elsewhere.
Dan Olson
Yes, you can always dereference if you cast into another type, but you can't do the same if you *don't* cast. In short, the compiler won't know what assembly instructions to use for math operations *until* you cast it.
ebencooke
I think GCC treats arithmetic on `void *` pointers the same as `char *`, but it's not standard and you shouldn't rely on it.
ephemient
A: 

No, it is not possible. What type should the dereferenced value have?

zvrba
A: 

In C, a void * can be converted to a pointer to an object of a different type without an explicit cast:

void abc(void *a, int b)
{
    int *test = a;
    /* ... */

This doesn't help with writing your function in a more generic way, though.

You can't dereference a void * with converting it to a different pointer type as dereferencing a pointer is obtaining the value of the pointed-to object. A naked void is not a valid type so derefencing a void * is not possible.

Pointer arithmetic is about changing pointer values by multiples of the sizeof the pointed-to objects. Again, because void is not a true type, sizeof(void) has no meaning so pointer arithmetic is not valid on void *. (Some implementations allow it, using the equivalent pointer arithmetic for char *.)

Charles Bailey
A: 

I want to make this function generic, without using ifs; is it possible?

The only simple way I see is to use overloading .. which is not available in C programming langage AFAIK.

Did you consider the C++ programming langage for your programm ? Or is there any constraint that forbids its use?

yves Baumes
Only C programming is to be used...
AGeek
yves Baumes
A: 
void abc(void *a, int b) {
  char *format[] = {"%d", "%c", "%f"};
  printf(format[b-1], a);
}
Is this program possible.... Please do check, if this possible in C programming...
AGeek
Yes, this is possible (although the code is dangerous without a range check for b). Printf takes a variable number of arguments and a is just pushed on the stack as any pointer (without any type information) and popped inside the printf function using va_arg macros using info from the format string.
hlovdal
At best this is not portable and will probably produce warnings left and right, at worst this is undefined behavior and who knows what happens.
SiegeX
+2  A: 

You should be aware that in C, unlike Java or C#, there is absolutely no possibility to successfully "guess" the type of object a void* pointer points at. Something similar to "getClass()" simply doesn't exist, since this information is nowhere to be found. For that reason, the kind of "generic" you are looking for always comes with explicit metainformation, like the "int b" in your example or the format string in the printf family of functions.

ammoQ
A: 

Void pointer known as generic ptr,which refers 2 variables of any data type .

Sneha