Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparison between pointer and integer ('int *' and 'int')

Tags:

objective-c

I am confused as to why I get this warning:

I intiate matchObsFlag with:

int *matchObsFlag=0;

but when I run this line:

    if (matchObsFlag == 1)

I get this warning. Any ideas?

like image 529
RGriffiths Avatar asked Sep 07 '13 12:09

RGriffiths


2 Answers

You surely get a warning because you did not cast 1 as such (int*) 1 so you test an equality between different things : an address and an int.

So it is either if(matchObsFlag == (int*)1) or if(*matchObsFlag == 1) depending on what you wanna do.

like image 70
LudoZik Avatar answered Oct 21 '22 05:10

LudoZik


int *matchObsFlag=0;

The type of matchObsFlag is int* while the constant literal is of type int. Comparison between the unrelated types is causing the warning.

matchObsFlag is a NULL pointer. matchObsFlag needs to point to a valid memory location if you wish to compare the value pointed by the pointer.

int number = 1;
matchObsFlag = &number;

Now, to compare the value, you need to dereference the pointer. So try -

if (*matchObsFlag == 1)
{
  // ...
}
like image 42
Mahesh Avatar answered Oct 21 '22 07:10

Mahesh