[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

g++ comparison between pointers and integers

Posted on 2000-02-28
4
Medium Priority
?
265 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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