Thread: while (char[ arraySize - 1] != '/0' ) Why can't I do this?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    while (char[ arraySize - 1] != '/0' ) Why can't I do this?

    I want to input into a char array and then have a while loop determine where the input terminates. I don't understand why I can't use the above statement though. My book was really vague about the char data type.


    6.33.c:11:38: warning: multi-character character constant
    6.33.c: In function `main':
    6.33.c:11: warning: comparison is always true due to limited range of data type

    Code:
    #include <stdio.h>
    
    int testPalindrome(char[], int, int);
    
    int main()
    {
       char string[ 15 ];
       int arraySize = 15, nSize = 15;
       scanf("&#37;s", &string);
    
       while( string[ arraySize - 1 ] != '/0' ) {
          --arraySize;
          printf("arraySize: %d\n", arraySize);
       }
    
       --arraySize;
       nSize = arraySize - 1;
    
       printf("arraySize: %d nSize: %d\n", arraySize, nSize);
       printf("%d\n", testPalindrome(string, nSize, arraySize) );
    
    
    return 0;
    }
    
    
    
    int testPalindrome(char array[], int n, int size)
    {
       if (size % 2 == 1 && (size - 1) / 2 == n ) {
          // TEST STATEMENT
          printf("if 1 n: %d\n", n);
          return 1;
       }
       else if (size % 2 == 0 && (size - 1) / 2 == n) {
          // TEST STATEMENT
          printf("if 2 n: %d\n", n);
          if (array[ n ] == array[ size - n ])
             return 1;
          else
             return 0;
       }
       else {
          // TEST STATEMENT
          printf("if 3 n: %d\n", n);
          if (array[ n ] == array[ size - n - 1 ])
             return testPalindrome(array, n - 1, size);
          else
             return 0;
       }
    
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    while( string[ arraySize - 1 ] != '\0' )
    You made a mistake there.

    ssharish2005

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are using the wrong slash, you should use '\0', not '/0' - the latter is two characters, the first is one (with the value zero).

    However scanning bacwards in an undefined string for a non-zero value is probably not a super-good idea. Use strlen() instead. It's a standard function, it's most likely faster than the way you would come up with anyways. And it's doing exactly what you want it to do: counting the number of letters in your string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    It appears that you're using gcc. I highly recommend always using at least the -Wall flag when building with gcc. It would have caught one other problem in your code: you should pass `string' or `&string[0]' to scanf(), not `&string'. Admittedly this is a fairly pedantic correction to an almost certainly benign bug, but a bug nevertheless. The main point, though, is that -Wall will provide you a lot more warnings, almost all of them useful.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    182
    --> matsp
    Do you think it's a good idea to be going beyond my book at this point though?


    --> cas
    What exactly does -wall do compared to pedantic?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by yougene View Post
    Do you think it's a good idea to be going beyond my book at this point though?
    If it is teaching you bad habits, you might even consider finding a different one.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by yougene View Post
    --> cas
    What exactly does -wall do compared to pedantic?
    -pedantic will warn about some gcc extensions; it makes the compiler a little more standards compliant. For example, `a ? : b' is valid in GNU C, but not standard C; -pedantic will warn you about that.

    -Wall will warn about all sorts of potential problems, even if the C standard doesn't necessarily require a diagnostic. For example, -Wall will check to see if your arguments to printf() match up with your conversion specifiers.

    If you're striving toward ANSI/ISO C compliance, a good set of options is: -Wall -std=c99 -pedantic

    You might prefer c89, tastes vary.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > If you're striving toward ANSI/ISO C compliance, a good set of options is: -Wall -std=c99 -pedantic

    GCC still doesn't have full C99 support (though it's getting close). So at present, C90 is the only C standard it's capable of complying with. On the other hand, it's good practice to write C code that complies with both C90 and C99 (the first because it's most common now, the second because it's the future). So -std=c99 might be useful in testing code to pick up some cases of C99 noncompliance, but is probably not a good idea to use by default.

    http://gcc.gnu.org/c99status.html

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I use
    Code:
    gcc -W -Wall -ansi -pedantic ...
    -W is the same as -Wextra.

    I usually stick in -g for debugging information as well.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by robatino View Post
    >So -std=c99 might be useful in testing code to pick up some cases of C99 noncompliance, but is probably not a good idea to use by default.
    Hence "tastes vary". =)

    Although gcc doesn't fully conform to C99, I unabashedly use some of the features that it does currently support. I've grown fond of compound literals, designated initializers, variable argument list macros, snprintf() (admittedly a library issue), stdbool, exact-width integers, among others.

    It really depends on what your code targets. If you're guaranteed a recent gcc or other C99-esque compiler (such as Intel's), using C99 features may not be a bad thing. Of course, if you want to be maximally portable, C89 is still the proper target. I just don't care much about C89 compliance when it comes to code for my own personal use.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    That still leaves a lot of C89 compilers out there, and just a few which pre-date the dinosaurs

    We try to stick to C89 here, so that we don't have to care which compiler someone is using, and anyone wandering by has a good chance of using the information.

    It is also fine to suggest C99 features if that would make the solution better, but it would be nice to add a "you need C99 for this" type disclaimer along side it.
    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.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    182
    So as a beginner it would be smart for me to stick with C89?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It would be smart of you to remain aware that there is a difference, and be aware of the language features you're using in your programs.

    In the end, you can do what you like. But hopefully, it will be an informed choice rather than just blindly going with whatever your compiler will let you get away with.
    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. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. string manipulation problem
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 10-06-2005, 06:13 AM
  3. Need help to understand this STL code.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2005, 01:59 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM