Assignment of void pointer variable
John S. Price
john at stat.tamu.edu
Sun Feb 18 22:55:48 AEST 1990
In article <19390 at grebyn.com> jmbj at grebyn.com (Jim Bittman) writes:
>This works in C, but I'd like to combine the last two lines...
> void *varptr[10];
> int *intptr;
> int myint;
> intptr = varptr[5];
> myint = *intptr;
>Goal: myint = (int) *varptr[5]; /* doesn't work, it's what I want! */
>Post or mail suggestions, Thanks for the help!
>Jim Bittman
>jmbj at grebyn.com
Umm... I believe this will work.
myint = *(int *)varptr[5];
The reason:
varptr is declared to be a void pointer, so you have
to tell the compiler what you want it to be "looking"
at when you dereference it. The (int *) cast tells the
compiler that the next pointer is a pointer to an integer,
and the outer dereference takes the integer at that location.
The reason yours didn't work is this:
myint = (int) *varptr[5];
You have a void pointer, defreference it,
and cast the result to an integer. This,
I assume, isn't what you wanted.
hope this helps, and I might be totally rambling, for
it is quite early in the morning...
--------------------------------------------------------------------------
John Price | It infuriates me to be wrong
john at stat.tamu.edu | when I know I'm right....
--------------------------------------------------------------------------
More information about the Comp.lang.c
mailing list