comparison between pointer and integer a problem?
This snippet of code:
Code:
#include <stdio.h> warning: comparison between pointer and integer Is this a problem or more appropriately bad form? Basically I'm wanting to test to make sure that the argument is a number, is there a better way to do this? |
Hi,
whats warning you is that you are comparing an int against a pointer (NULL). That doesnt have a lot of sense besides atoi doesnt not return NULL. Acording to the man page, atoi doesnt detect errors. So be careful with it, or use strtol :) Hope this is useful. Cheers! |
Quote:
Code:
#include <stdio.h> |
a is an int.
NULL is a pointer When you compare the two, you are comparing two different types. That is what the compiler warns you for. The warning is there to warn you about possible errors you might have made. The fun thing is, when a=0, a==NULL would return true anyway, since (int)NULL = 0 indeed. Apart from the fact the (a==NULL) is syntactically not quite correct (but could be acceptable in certain cases) the problem is that your assumption that atoi returning 0 at an error is not right. atoi does not do error checking at all, and it does certainly not return NULL at an error. Not a pointer, but maybe the interger 0. This might just be implementation dependent so you program might not work when compiled on a different platform of a different [version of] compiler. If you do "man strtol" on your bash command line you see the syntax for that command, and you also see that it defines a parameter which can return an error indication. It is a bit more complicated than atoi, but it makes your program fail-safe. jlinkels |
I understood why I got the warning, but now I understand the reasoning behind it. I tried reading the man page for strtol, I even googled for examples, and I still can't figure out how to work it in to this program. I'm just now starting to grasp the concept of pointers. Maybe someone could help me with an implementation in this instance that would verify that the argument is infact a number?
It would be much appreciated. I'm glad you told me about the NULL thing though, I guess I wasn't quite grasping that. Trying to learn C in my spare time, and I'm really struggling with some logic decisions. Did you think the rest of the code looked functionable though? Not insanely verbose or anything? |
Quote:
|
During the conversion strtol and strtoul go through the string till they find a string terminator or an invalid character. They then stop and the argument endptr contains the last digit that was processed. For a valid number, you expect '\0'.
Please note that the functions can work with decimal (base 10) and hexadecimal (base 16) 'numbers'. You can pass different strings to the program below (digits, mixed digits and text) and see how it behaves. Code:
#include <stdlib.h> |
Thank you so much Wim. Trying to learn C on your own when you have little to no programming experience can be challenging, but I really feel that it is the one I want to start with. I know it's powerful, and I don't yet want to jump into the OOP of C++ and JAVA. I appreciate the help.
jineshkj - I just went out and got the C Programming Language to help me along, I was trying internet tutorials, and it just wasn't working, so hopefully it will help me along a bit better. |
All times are GMT -5. The time now is 08:09 AM. |