char array size question, please help!

This is a discussion on char array size question, please help! within the C Programming forums, part of the General Programming Boards category; Anyone happen to know why this is? Thanks, Ash Code: /* Anytime you want to use input validation, always make ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    char array size question, please help!

    Anyone happen to know why this is? Thanks, Ash

    Code:
    /* Anytime you want to use input validation, always make the char array 2 bigger than what you need or it won't work.I'm
    not sure why this is because it seems like the program below would work, but it you don't change str[6] to str[7], it
    doesn't.
    When fgets gets a string it will save 1 spot for the \0, and you'll need a space for the \n as well.
    That's why in the program below which wants a 5 digit number, it seems like using "char str[6]" would work fine.
    That should mean that fgets will also save str[6] for the \0 and str 0 - 4 would be for the 5 digit number
    and str[5] would be for the \n, but this doesn't work, only if you change "char str[6]" to "char str[7]", but why???
    
    
    */
    
    #include <stdio.h>
    
    int main() {
    
    int lp;
    int cb;
    char overflow = 'x'; //to make sure it doesn't get assigned something else when defined.
    int input;
    int nchars;
    int returncode;
    char str[6];
    
    printf("Enter a 5 digit number:");
    
    fgets(str, sizeof(str), stdin);  //fgets will save str[6] for the \0
    
    if (str[5] != '\n') {
       printf("Not a 5 digit number\n");
    
       for (lp = 0;lp < sizeof(str); lp++) {
           if (str[lp] == '\n') {
              overflow = 'F';
              }
              }
        if (overflow != 'F') {
           printf("Overflow = T\n");
           while ((cb=getchar()) != '\n');
           }
        main();
       }
    
       //
    
    for (lp = 0; lp < sizeof(str); lp++) {
        if (str[lp] == '\n') {
           str[lp] = '\0';
                             }
           }
    
    
    
    returncode = sscanf(str, "%d%n", &input, &nchars);
    
    if (returncode != 1) main();
    
    if ((returncode == 1) && (str[nchars] != '\0')) main();
    
    else printf("Good job!");
    
    getchar();
    exit(0);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    str[6] contains
    1 2 3 4 \n \0
    That's 6 chars in total - but you only typed in 4 actual characters of data.

    Also, please learn how to use loops, and not call main() recursively.

  3. #3
    old man
    Join Date
    Dec 2005
    Posts
    90
    Quote Originally Posted by Ash1981
    That should mean that fgets will also save str[6] for the \0 and str 0 - 4 would be for the 5 digit number
    and str[5] would be for the \n, but this doesn't work, only if you change "char str[6]" to "char str[7]", but why???
    With 'char str[6]' a reference to str[6] is outside of your array. Remember that the numbering is zero-based: in this case 0-5

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    if (returncode != 1) main();
    
    if ((returncode == 1) && (str[nchars] != '\0')) main();
    In C++ it's illegal to call main(). In C you can do it, but you should probably use a loop instead.
    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.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    thanks all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of strings question
    By FortinsM3 in forum C Programming
    Replies: 17
    Last Post: 02-15-2009, 03:02 PM
  2. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 06:33 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 04:16 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21