-
September 12th, 2024, 01:22 PM
#1
methods to fix "ordered comparison of pointer to integer 0" error, gcc 12.2
Hello,
I am rebuilding an older project. The last time I built this was with gcc/g++/gfortran 4.6. Rebuilding under gcc/g++gfortran 12.2 I received the "ordered comparison of pointer to integer 0" error for the following code.
Code:
const int GBBUFSIZE = 512;
char getbuf[GBBUFSIZE];
else if( getbuf[0] == '>' && (strchr(getbuf, '<') > 0 || strstr(getbuf, "DT") > 0 ))
The version 12.2 compiler doesn't like the strchr(getbuf, '<') > 0 or strstr(getbuf, "DT") > 0 evaluations.
The strchr() function accepts a string/char array and a char(s) and returns a pointer to the first occurrence of the char in the array. The function returns NULL if the char is not found. I suspect that this is a pointer that points to NULL but I don't know.
I didn't write this code but my interpretation of the above is that strchr(s, c) > 0 means that the char c was found in the string s so the condition evaluates true if the first char of the string getbuf is ">" and the string also contains either "<" or "DT". I don't remember everything about order of evaluation so please let me know if this is not correct.
There are a few things I can think of to get this to compile.
// use nullptr which I believe is the gcc11+ method
Code:
else if( getbuf[0] == '>' && (strchr(getbuf, '<') != nullptr || strstr(getbuf, "DT") != nullptr ))
// use NULL, which I think would work
Code:
else if( getbuf[0] == '>' && (strchr(getbuf, '<') != NULL || strstr(getbuf, "DT") != NULL ))
// declare a pointer to NULL and use that, not sure how this is different than just using NULL
Code:
char* test_char = NULL;
else if( getbuf[0] == '>' && (strchr(getbuf, '<') != test_char || strstr(getbuf, "DT") != test_char ))
I have some questions, in no particular order.
1. Do any of the above solutions inadvertently change the logic of the conditional?
2. Which, if any, of these is the most portable?
3. Are any of these solutions a bad idea, might lead to unexpected behavior, etc?
4. Is there a better solution I didn't include?
I did test compile this using nullptr and it does at least compile. I guess I would like to know if one of these solutions would still work with gcc4 or if I have to add a compiler directive so support the different compiler versions.
Thanks,
LMHmedchem
-
September 12th, 2024, 11:28 PM
#2
Re: methods to fix "ordered comparison of pointer to integer 0" error, gcc 12.2
1. Do any of the above solutions inadvertently change the logic of the conditional?
No.
2. Which, if any, of these is the most portable?
The ones that don't use nullptr.
Are you using a C compiler or C++ compiler?
https://en.cppreference.com/w/c/keyword/nullptr only just recently added to C
https://en.cppreference.com/w/cpp/keyword/nullptr present in C++ for over a decade
3. Are any of these solutions a bad idea, might lead to unexpected behavior, etc?
char* test_char = NULL is just unnecessary extra verbiage.
4. Is there a better solution I didn't include?
You get the != NULL for free just by doing this:
Code:
else if( getbuf[0] == '>' && (strchr(getbuf, '<') || strstr(getbuf, "DT")) )
-
September 18th, 2024, 09:32 PM
#3
Re: methods to fix "ordered comparison of pointer to integer 0" error, gcc 12.2
Sorry for the delay. Thanks for the great post. I wish that every answer I got was as clear and well explained.
What you suggest in answer 4 is the kind of thing I just never think of. I will probably go with,
Code:
else if( getbuf[0] == '>' && (strchr(getbuf, '<') != NULL || strstr(getbuf, "DT") != NULL ))
because I will be more likely to remember what this is doing the next time I read it.
Thanks again,
LMHmedchem
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|