37

I started feeling comfortable with C and then I ran into type casting. If I have the following defined in an *.h file

struct data {
    int value;
    char *label;
};

and this in another *.h file

# define TYPE      void*

How do I cast the void pointer to the struct so that I can use a variable "TYPE val" that's passed into functions? For example, if I want to utilize the value that TYPE val points to, how do I cast it so that I can pass that value to another functions?

user1852050
  • 655
  • 2
  • 13
  • 17
  • `#define TYPE void*` is a bad idea. It at least should be a `typedef` instead. Consider what would happen in the following declaration: `TYPE x, y;` or `const TYPE x;`. – jamesdlin Feb 18 '13 at 22:46
  • 2
    Also, if you're dealing with `void*` pointers and C, you don't need to do any explicit casting. C allows `void*` pointers to be *implicitly* cast: `void* p = ...; struct data* val = p;`. – jamesdlin Feb 18 '13 at 22:48
  • @jamesdlin I agree that it's a bad idea, but it's not my idea, so I have to go with it. So along with that, when `TYPE val` is passed into a function as a void pointer, I can simply declare `struct data* temp = val;` then pass temp to a function and I should be able to then use temp->value? – user1852050 Feb 18 '13 at 23:38
  • 1
    Yes, you then could use `temp->value`. – jamesdlin Feb 19 '13 at 00:15
  • Thanks, your answer was simple and helpful. If it were posted in the answer section, I'd give it an upvote. – user1852050 Feb 19 '13 at 00:27

2 Answers2

42
(struct data*)pointer

will cast a pointer to void to a pointer to struct data.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • Maybe I'm beating this to death, but in the above example this would mean that in order to cast TYPE val to the struct pointer so I can get at the value it points to, I would do this: (struct data*)val? What exactly does this do? And if I went ahead and declared a variable 'int num = (struct* data)val' would that get the number val points to or just the memory address that val points to? If the latter, how do I cast it to get at value of 'val' Thanks for your patience. – user1852050 Feb 18 '13 at 22:24
  • @user1852050 you are confusing struct variables and pointer-to-struct variables. When you need to get a pointer to a struct you can use the `&` operator. – wRAR Feb 18 '13 at 22:33
  • So I could declare and allocate memory for a temp struct and then give it the value of &val? As in temp = &val. And then get the value by using temp->value? – user1852050 Feb 18 '13 at 22:43
  • You don't seem to understand pointers. Pointers to things are like addresses of houses on the street, but they aren't the houses themselves. To get the address of a `house` in C you do `&house`. To get the `house` by its address/pointer in C you do `*pointer`. If `pointer` has been previously obtained as `pointer = &house;` and if `pointer` points to type of `house`, then `*pointer` is equivalent to `*(&house)` and ultimately to `house`. – Alexey Frunze Feb 18 '13 at 22:55
  • Pointers to some non-void type `T`, unlike pointers to `void`, can be dereferenced (that's what we did with `*pointer`) and in the process return an object of type `T`. So, if you convert a pointer to the type of `house` (pointing to one `house`) to, say, pointer to the type of `castle`, then dereferencing the converted pointer will not give you an object of type `castle`, because really the pointer still points to a `house`. – Alexey Frunze Feb 18 '13 at 22:57
  • Now, pointers to structures can be dereferenced not only to entire structures, but further to individual structure members. So, if you have a pointer to a structure representing the parts of a house, then you can dereference it with `->` to get those parts, e.g. by doing something like `pointer->kitchen`, which is equivalent to `(*pointer).kitchen`, e.g. first get the whole thing, then extract `kitchen` from it. – Alexey Frunze Feb 18 '13 at 22:59
  • You can get pointers to those individual things as well, e.g. `&(pointer->kitchen)` (or simply `&pointer->kitchen`) will give you a pointer to `kitchen` contained within some `house` that `pointer` points to. – Alexey Frunze Feb 18 '13 at 23:04
  • @Alexey Frunze Thanks, that all makes sense and gives me some good visualizations. Okay, I've been running '(struct data*)val' through debugger and watching it as I pass it into a function (I'm a visual person). The memory address is moving into the function. However, it still shows in my watch panel that it is a void pointer, which means the compiler I'm using will not allow me to grab the elements 'value' and 'label'. Clearly I'm casting incorrectly so that as the void pointer passes it into the new function it is not becoming a struct pointer. What am I doing wrong? – user1852050 Feb 18 '13 at 23:29
  • If the function declares its parameter as `void*`, you aren't going to see it as a pointer to something else inside of the function. In the function, you need to declare a pointer to what-you-want-to-see and assign it the value of that `void*` parameter. Only then (and perhaps when you disable all optimizations) will you be able to peek into what the pointer points to. – Alexey Frunze Feb 18 '13 at 23:38
3

Typecasting void pointer to a struct can be done in following

void *vptr;
typedef struct data
{
   /* members */
} tdata;

for this we can typecast to struct lets say u want to send this vptr as structure variable to some function

then

void function (tdata *);
main ()
{
    /* here is your function which needs structure pointer 
       type casting void pointer to struct */
    
    function((tdata *) vptr);
}

Note: we can typecast void pointer to any type, thats the main purpose of void pointers.

pevik
  • 4,523
  • 3
  • 33
  • 44
Mahadev
  • 71
  • 4
  • 1
    why was this downvoted? it brings up a few valid points (e.g. `we can typecast void pointer to any type`, for example), and it does answer the question (albeit not very clearly) – Jacob Garby Jun 04 '18 at 17:33