From: Robby on 22 May 2010 10:43 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
From: Robby on 22 May 2010 10:54 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 "Robby" wrote: > 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 >
From: Scott McPhillips [MVP] on 22 May 2010 11:01 "Robby" <Robby(a)discussions.microsoft.com> wrote in message news:5C911160-0A5B-4D72-8E3D-3F1DE6A04031(a)microsoft.com... > 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. 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]
From: Barry Schwarz on 22 May 2010 11:43 On Sat, 22 May 2010 07:54:01 -0700, Robby <Robby(a)discussions.microsoft.com> wrote: >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? 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
From: Robby on 22 May 2010 12:01 > 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.
|
Next
|
Last
Pages: 1 2 Prev: Reminder - Microsoft Responds to the Evolution of Community Next: LPCWSTR vs. const LPWSTR |