Discussion:
"PORTING C" > help comparing a pointer's value to NULL ?
(too old to reply)
Robby
2010-05-22 14:43:01 UTC
Permalink
Hello,

Could someone please help me with comparing the contents of a pointer and
NULL.

I don't know why I am confused with this. Pointers can be assigned to NULL
right. And if I want to compare that NULL how do we do it.

Here is the snippet... it compiles but with a warning such as:
MN.c:85: warning: comparison between pointer and integer

The variables "curr_icon_pos, SIZESFLI and Fli" are set in my real program
but did not set them here since they are irrelevant to the issue.

Please note that,

Sometimes the calling function is like this:
=======================================
int main()
{
unsigned short folderIdx = 4;
mn_get_index_to_prev_fli_icon (0, &folderIdx, curr_icon_pos, SIZESFLI, Fli);
return 0;
}
=====================================

and other times its like this where I just assign a NULL as the 2nd parameter:
=======================================
int main()
{
mn_get_index_to_prev_fli_icon (2, NULL, curr_icon_pos, SIZESFLI, Fli);
return 0;
}
=====================================

============================Function implementation
short mn_get_index_to_prev_fli_icon
( unsigned char offset,
unsigned short *icon_idx,
unsigned short curr_icon_pos,
unsigned char arr_size,
unsigned short *array)
{
unsigned char x;
unsigned short rValid = FALSE;

for(x=0; x < arr_size; x++)
{
if(array[x] == curr_icon_pos)
{ rValid = TRUE;

if((*icon_idx) != NULL)
{
*icon_idx = (unsigned short) (x + offset);
}
break;
}
}
return rValid;
}
=========================================

Please note "x" and "arr_size" should be an int but for the time being while
I am porting I left all variables to the their 8 bit MCU's corresponding
width.


I am getting a warning on the following line:

if((*icon_idx) != NULL)

where if the contents of the pointer is not NULL, then I will dereference
the pointer so I can assign a value to it.

all help appreciated!

Thanks
Roberto
Robby
2010-05-22 14:54:01 UTC
Permalink
okay guys, I was able to get rid of the warning by casting the NULL to the
same type the pointer was... like this:

if((*icon_idx) != (unsigned short) NULL)

but is this the best way of doing this sort of stuff?
--
Best regards
Roberto
Post by Robby
Hello,
Could someone please help me with comparing the contents of a pointer and
NULL.
I don't know why I am confused with this. Pointers can be assigned to NULL
right. And if I want to compare that NULL how do we do it.
MN.c:85: warning: comparison between pointer and integer
The variables "curr_icon_pos, SIZESFLI and Fli" are set in my real program
but did not set them here since they are irrelevant to the issue.
Please note that,
=======================================
int main()
{
unsigned short folderIdx = 4;
mn_get_index_to_prev_fli_icon (0, &folderIdx, curr_icon_pos, SIZESFLI, Fli);
return 0;
}
=====================================
=======================================
int main()
{
mn_get_index_to_prev_fli_icon (2, NULL, curr_icon_pos, SIZESFLI, Fli);
return 0;
}
=====================================
============================Function implementation
short mn_get_index_to_prev_fli_icon
( unsigned char offset,
unsigned short *icon_idx,
unsigned short curr_icon_pos,
unsigned char arr_size,
unsigned short *array)
{
unsigned char x;
unsigned short rValid = FALSE;
for(x=0; x < arr_size; x++)
{
if(array[x] == curr_icon_pos)
{ rValid = TRUE;
if((*icon_idx) != NULL)
{
*icon_idx = (unsigned short) (x + offset);
}
break;
}
}
return rValid;
}
=========================================
Please note "x" and "arr_size" should be an int but for the time being while
I am porting I left all variables to the their 8 bit MCU's corresponding
width.
if((*icon_idx) != NULL)
where if the contents of the pointer is not NULL, then I will dereference
the pointer so I can assign a value to it.
all help appreciated!
Thanks
Roberto
Barry Schwarz
2010-05-22 15:43:44 UTC
Permalink
On Sat, 22 May 2010 07:54:01 -0700, Robby
Post by Robby
okay guys, I was able to get rid of the warning by casting the NULL to the
if((*icon_idx) != (unsigned short) NULL)
but is this the best way of doing this sort of stuff?
No, it actually the absolute worst way. Using a cast to silence a
warning is almost always a mistake. There is a reason the compiler is
issuing this warning; your code attempts to perform a constraint
violation. Do you have a really good reason for trying to violate the
constraint? Do you recognize what the violation is? If not, why are
you telling the compiler it is OK to do it anyway?

What will happen in this case if icon_idx is NULL? What is the only
possible result of dereferencing a NULL pointer?
--
Remove del for email
Scott McPhillips [MVP]
2010-05-22 15:01:26 UTC
Permalink
Post by Robby
if((*icon_idx) != NULL)
where if the contents of the pointer is not NULL, then I will dereference
the pointer so I can assign a value to it.
That is not testing the pointer for NULL, it is testing the pointee for
NULL.

You can directly compare a pointer to NULL
if (icon_idx != NULL)

or even
if (icon_idx)
--
Scott McPhillips [VC++ MVP]
Robby
2010-05-22 16:01:01 UTC
Permalink
Post by Scott McPhillips [MVP]
That is not testing the pointer for NULL, it is testing the pointee for
NULL.
You can directly compare a pointer to NULL
if (icon_idx != NULL)
or even
if (icon_idx)
Yeah... I just remembered the subtle difference:
If we do this:

int y;
int *x;

x = &y;

So therefore, in memory, we have:

[x] holding [address of where y resides]

But when we do this:
int *x = NULL;

in memory we have:

[x] holding [a NULL which really isn't any address of a variable]

Just as we test if a pointer is valid from malloc we do the same here...we
test what x is holding!

hence:
if(x!=NULL)

I have been working on electronic hardware the last few months.. funny how I
quickly forget these programming details.

In anycase, I thankyou for your reply Scott.
Barry Schwarz
2010-05-22 15:43:44 UTC
Permalink
On Sat, 22 May 2010 07:43:01 -0700, Robby
Post by Robby
Hello,
Could someone please help me with comparing the contents of a pointer and
NULL.
I don't know why I am confused with this. Pointers can be assigned to NULL
NULL is not a variable. Therefore, it is never possible to assign
anything to it. Pointers can only be assigned to other pointers.

On the other hand, NULL is intended to be a pointer value (on some
systems its actual type is an integer type but that does not change
the intent) and it can be assigned to a pointer.
Post by Robby
right. And if I want to compare that NULL how do we do it.
The same way you do any other comparison. Use the == or != comparison
operator.
Post by Robby
MN.c:85: warning: comparison between pointer and integer
The variables "curr_icon_pos, SIZESFLI and Fli" are set in my real program
but did not set them here since they are irrelevant to the issue.
Please note that,
=======================================
int main()
{
unsigned short folderIdx = 4;
mn_get_index_to_prev_fli_icon (0, &folderIdx, curr_icon_pos, SIZESFLI, Fli);
return 0;
}
=====================================
=======================================
int main()
{
mn_get_index_to_prev_fli_icon (2, NULL, curr_icon_pos, SIZESFLI, Fli);
return 0;
}
=====================================
============================Function implementation
short mn_get_index_to_prev_fli_icon
( unsigned char offset,
unsigned short *icon_idx,
unsigned short curr_icon_pos,
unsigned char arr_size,
unsigned short *array)
{
unsigned char x;
unsigned short rValid = FALSE;
for(x=0; x < arr_size; x++)
{
if(array[x] == curr_icon_pos)
{ rValid = TRUE;
if((*icon_idx) != NULL)
{
*icon_idx = (unsigned short) (x + offset);
}
break;
}
}
return rValid;
}
=========================================
Please note "x" and "arr_size" should be an int but for the time being while
I am porting I left all variables to the their 8 bit MCU's corresponding
width.
You want to tell us what the warning is?
Post by Robby
if((*icon_idx) != NULL)
Why are you dereferencing the pointer? At this point, you are not the
least bit interested in the value pointed to, only if the pointer
points to an object you can use.
Post by Robby
where if the contents of the pointer is not NULL, then I will dereference
the pointer so I can assign a value to it.
--
Remove del for email
Robby
2010-05-22 20:06:01 UTC
Permalink
Post by Barry Schwarz
Post by Robby
if((*icon_idx) != NULL)
Why are you dereferencing the pointer? At this point, you are not the
least bit interested in the value pointed to, only if the pointer
points to an object you can use.
My error. I took care of this inmy last post:

if(x!=NULL)


regards
robert

Loading...