1

Possible Duplicate:
Dereferencing void pointers

I have a function call this way:

void foo(void *context)   //function prototype
..
..
..
main()
{
.
.
foo(&(ptr->block));  //where ptr->block is of type integer.
.
.
.
void foo(void *context)
{
 Here I try to use the ptr->block but am having problems. I have tried 

 if((int *)context ==1)  
  ..
  ..
}

I am typecasting it back to int in the function to use it. Am I dereferencing it wrong inside the foo() function?

Community
  • 1
  • 1
mane
  • 2,093
  • 4
  • 16
  • 14

3 Answers3

5
if((int *)context ==1)

you want this instead:

if(*(int *)context ==1)

You were casting to a pointer to int but what you actually want is to actually access the int so you need to dereference the pointer.

ouah
  • 142,963
  • 15
  • 272
  • 331
3

You didn't typecast it to int, you typecast it to int *, and then didn't dereference it. Try:

if (*(int *)context == 1)
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

when you want to access the value, just casting the pointer wont help you..

(*(int*)context == 1) will solve your issue..

Raghu Srikanth Reddy
  • 2,703
  • 33
  • 29
  • 42