Thread: 1 code warning I need help with...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    28

    1 code warning I need help with...

    My program compiles without any errors and runs perfectly, but I am getting a warning that I'm not sure about if you guys can give me some advice it would be most appreciated...

    It says "warning: comparison between pointer and integer" I understand that it doesn't like: [I]firstname != NULL ... Well, what's odd is firstname[i] isn't a pointer, it's an array.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int tolower(int c);
    
    int main(int argc, const char * argv[]) {
    
    int i;
    char firstname[20];
    	
    	strcpy( firstname, "STEVEN" );
    	
    		for ( i = 0 ; firstname[i] != NULL ; i = i + 1 ) {
                    // warning: comparison between pointer and integer
    
    		printf( "%c", tolower( firstname[i] ) ); 
    
    		}
    	
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    firstname[i] is a char, not an array. firstname is the array.

    The problem is that (on your system) NULL has type void*. NULL should be used in pointer context only. What you want to compare against is the null character, which you can represent with 0 or '\0'.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    I changed NULL to simply 0 and I didn't get a warning. That makes sense...

    Though I'm surprised my for loop worked because the condition is keep looping while firstname[i] is not zero, yet i initalize i as 0 and it doesn't care.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lucidrave View Post
    Well, what's odd is firstname[i] isn't a pointer, it's an array.
    Wrong. As cas says, firstname[i] is a char (so niether an array nor a pointer).

    The reason that the code works is that NULL is usually defined as having a value of 0 (but that is not guaranteed by the C standard).

    printf("%d",NULL)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by lucidrave View Post
    Though I'm surprised my for loop worked because the condition is keep looping while firstname[i] is not zero, yet i initalize i as 0 and it doesn't care.
    What doesn't care? Do you mean the loop is still entered?

    The value of i is not what you're testing, so the value of i doesn't (directly) matter. You're testing firstname[i] which is a different thing altogether. If you had this:
    Code:
    for(i = 0; i != 0; i++) ...
    then the loop would not be entered. But you're not doing that; you're testing firstname[i] against 0. When i is 0, you're checking firstname[0], which is 'S' in your program. Then you test firstname[1], which is 'T'; and so on. In C, a string is a collection of characters terminated by a byte with the value zero. So your loop is checking each character of the string until it finds a byte whose value is zero. Once this value is found, you're at the end of the string and so you want to finish looping.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    Ahh yes! That makes total since... It should have been obvious i is not the same as firstname[i]

    Although another odd thing... If I put in the name "STEV0000EN"

    at some point firstname will != 0 not because of the Null 0 but the literal 0 in "STEV0000EN" the program continues to work fine though even if I use that weird name. Trying to figure out why

    Quote Originally Posted by cas View Post
    What doesn't care? Do you mean the loop is still entered?

    The value of i is not what you're testing, so the value of i doesn't (directly) matter. You're testing firstname[i] which is a different thing altogether. If you had this:
    Code:
    for(i = 0; i != 0; i++) ...
    then the loop would not be entered. But you're not doing that; you're testing firstname[i] against 0. When i is 0, you're checking firstname[0], which is 'S' in your program. Then you test firstname[1], which is 'T'; and so on. In C, a string is a collection of characters terminated by a byte with the value zero. So your loop is checking each character of the string until it finds a byte whose value is zero. Once this value is found, you're at the end of the string and so you want to finish looping.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lucidrave View Post
    Ahh yes! That makes total since... It should have been obvious i is not the same as firstname[i]

    Although another odd thing... If I put in the name "STEV0000EN"

    at some point firstname will != 0 not because of the Null 0 but the literal 0 in "STEV0000EN" the program continues to work fine though even if I use that weird name. Trying to figure out why
    The character '0' is not 0. (It is 48, in the ASCII world.)

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    Ok I understand.

    Thanks for the help on that everyone!

    Quote Originally Posted by tabstop View Post
    The character '0' is not 0. (It is 48, in the ASCII world.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  3. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  4. Confusion : Warning long code
    By mart_man00 in forum C Programming
    Replies: 10
    Last Post: 04-08-2003, 08:07 PM
  5. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM

Tags for this Thread