Solved

g++ comparison between pointers and integers

Posted on 2000-02-28
4
259 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Mini is no longer serving everything 7 154
yum update fails 11 378
init script for deployment on linux machine 5 52
WiFi Router device supports GPON! 3 29
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now