Solved

g++ comparison between pointers and integers

Posted on 2000-02-28
4
260 Views
Last Modified: 2010-04-22
I have existing code that I'm trying to port to Linux and it's becoming a real pain that g++ doesn't allow comparison between pointers and integers.  The specific problem I keep running into is using NULL to be integer value 0.  For example, the following code won't compile

int get_int(){ ... }

int main(){
  if(get_int() == NULL) ...
}

because it says that ANSI C++ forbids comparison between pointers and integers.  The -traditional flag does the trick if compiling C code, but doesn't work with C++ code.

The code compiles fine using the compilers on NT, AIX, and Solaris.

Is there a way to get g++ to allow this?

Thanks,
Rich
0
Comment
Question by:relleh
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
jlevie earned 50 total points
ID: 2565949
Make the test like:

if(get_int() == (int)NULL)

NULL is defined to be null pointer and get_int() is of type int.
0
 

Author Comment

by:relleh
ID: 2566452
I was hoping g++ had a command line option that told it to allow the comparison.  The problem is that there are too many places in the code where that sort of thing is done.  I could also make the test be

if(get_int() == 0)

and completely get rid of NULL, but that takes a lot of time digging through all the code.  Also, because the other programmers already have the habit of using NULL that way, when new code is added it's likely to show up again.

Thanks,
Rich
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2566525
Well, yeah, it's a pain to fix and you have to keep beating people over the head to get them to do things the right way, but there's no guarantee that the next release of Sun's C++ (or somebody else's) won't take the same view that g++ does.

<personal rant mode on> If the code violates the standard, it ought to be changed, period. I've spent entirely too many hours fixing non-compliant codes that worked by a fluke on one platform/compiler/version and failed miserably on another. <personal rant mode off>.
0
 
LVL 2

Expert Comment

by:curri
ID: 2570248
I'd suggest you change all NULL's to 0's. Remember, this does NOT have to be done by hand !!! you can use sed, or your favorite tool to do that !!!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question