1
votes

I have stored an int in a void * variable in a struct

void * data = &ID;

and now I want to print it. How would I do this? I tried to dereference it but the compiler complained about dereferencing void*. Thanks!

3
*(int*)data, if initial type was int. - keltar
show the code you are using to deference it - Ido Sarig
clarify whether ID is the int you are talking about - M.M

3 Answers

1
votes

You should cast data to int * first, then derefer it:

printf("%d\n", *(int *)data);
0
votes

You need to typecast your void* to int* first. Then you need to print value of that pointer means need to dereference it.

printf("Data: %d\n", *((int *)data));
0
votes

ya just cast and dereference

printf("%d\n", *(int *)data);