Solved

g++ comparison between pointers and integers

Posted on 2000-02-28
4
258 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

17 Experts available now in Live!

Get 1:1 Help Now