?
Solved

g++ comparison between pointers and integers

Posted on 2000-02-28
4
Medium Priority
?
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
jlevie earned 150 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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