Thread: Can someone please explain this error?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    Can someone please explain this error?

    Code:
     void strCase(char s[])
                   {
                        int i;
                       while (s[i] != NULL)
                        {
                              s[i] = toupper(s[i]);
                              i++;
                        }
                   }
    I am basically saying as long as the position in the array is not NULL

    make the character a capital

    but why am I not allowed to compare an integer (i) and a pointer (Null)??

    [Warning] comparison between pointer and integer

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    NULL should only be used in pointer context because it's often defined as ((void*)0). What you're checking for is the null character ('\0'):
    Code:
    while (s[i] != '\0')
    The specific meaning of your error is that char is an integer type and NULL is a pointer type. The warning says that you're probably not doing what you think you're doing, which is correct.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    I got it to work but it still isn't capitalizing all my letters not sure why :\

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Try initializing i.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Quote Originally Posted by anduril462 View Post
    Try initializing i.
    where would I initialize 'i' in my code??

    When I take in the input or when I re-display it?

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Presumably you want your i variable to have a defined value before you enter the loop. This value is probably 0.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by cprogrammer1980 View Post
    where would I initialize 'i' in my code??

    When I take in the input or when I re-display it?
    You SHOULD initialize ALL variables before you use them.

    Some instructors, want them initialized when you declare them.

    Tim S.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    now in the code below would this work for initializing i. I did it after declaring it

    Code:
                   int strCase(char s[])
                   {
                        int i;
                         for (i=0; s[i]!='\0';i++)
                              s[i] = toupper(s[i]);
                   }
    also using a while loop
    Code:
                   int strCase(char s[])
                   {
                              int i=0;
                              while(s[i]!='\0')
                              {
                                               s[i] = toupper(s[i]);
                                               i++;
                              }
                   }
    Last edited by cprogrammer1980; 03-25-2011 at 12:03 PM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You presently have two near identical threads running... Pick ONE and stay with it...

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock problem
    By Wolf` in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2010, 04:55 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM