Thread: Array length problem

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    22

    Array length problem

    I'm trying to set up two arrays that will store the surname and forename of a person when prompted to enter it. However, because the name could be anything when entered, I don't know what size to make the array. Will I have to do a dynamic array for this, or is there an easier way to do this?

    The way I've done it before is based on an example in my text book that tells me that I could do a string as follows:

    Code:
    char name[] = "John Smith";
    , which would create an array with 11 objects of type char.

    I figured I could do a similar thing that would have the same idea, but that looks like this,
    Code:
    char name[];
    and then have a scanf where the value was entered, but then it won't compile properly.

    If you understand my ramblings - please help me!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char name[] = "John Smith";

    asks a compiler to count number of bytes in the string "John Smith" add one byte for the 0 character, allocate enouhg space for it and copy this string into allocated buffer.

    char name [11];
    asks compiler to allocate 11 bytes for later use

    char name[]; ask compiler to allocate a buffer but does not provide any information about desired buffer size. so compiler shows error that he cannot determine the buffer size to allocate
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    when i confront this situation, i often allocate a large enough space in the stack or the heap to store the data.
    or you can use the dynamic array for this. input the data into the dynamic array character by character, when you find the array is not enough, reallocate it with a large size, then copy the characters to the new array, delete the original array
    Hello, guys, i come from china, so i am not good at english. If you find the sentence i wrote full of mistakes, please tell me.
    I confirm that my english and programming skills will be improved with your help in this forum. thanks^_^

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    Quote Originally Posted by vart
    char name[] = "John Smith";

    asks a compiler to count number of bytes in the string "John Smith" add one byte for the 0 character, allocate enouhg space for it and copy this string into allocated buffer.

    char name [11];
    asks compiler to allocate 11 bytes for later use

    char name[]; ask compiler to allocate a buffer but does not provide any information about desired buffer size. so compiler shows error that he cannot determine the buffer size to allocate
    Yeah I had a feeling thats how it works, just wondered if there was a way of doing it in that sort of manner. I know I could allocate a large amount of memory to it, but that seems like rather a waste of memory. I could also (this is the way I'm trying) ask the user how many letters are in the word that is being inputted. I don't really like this way, and I was just wondering if there is a way to count the number of letters as they're being entered and then assigning the memory to that?

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You could just dynamically resize your array every time they input a new character. But that would be a waste of computation. Pick your poison, I guess.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    That doesn't sound like too much fun! How I said above that I was going to do it doesn't work. It compiles first, but it now crashes, which isn't a whole lot of laughs either. I might just have a large amount of memory allocated for it. Might be easier.

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Yeah, and I doubt lack of memory is that big of an issue for your computer.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    Not so much no. I'm sure it can cope with about 40 extra bytes being used maybe?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    How are you reading in "John Smith" from the user? I bet you're using something like this:
    Code:
    scanf("%s", name);
    scanf("%s") only reads until the first whitespace character. That is, "John" will be stored in name, and "Smith" will remain in the input stream. This could cause a problem if, for example, you read John Smith's age right afterwards; scanf() will see "Smith" and conclude that that isn't a number (which it isn't), and return an error code (which you probably don't check for), leaving the age variable unchanged.

    To read spaces into name, use fgets() instead of scanf(). http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Code:
    #include <stdio.h>  /* for BUFSIZ and fgets() */
    #include <string.h>  /* for strchr() */
    
    char name[BUFSIZ], *p;
    fgets(name, sizeof(name), stdin);
    if((p = strchr(name, '\n')) != 0) *p = 0;  /* remove the newline from name */
    When reading numbers from the user, you could discard non-digit input, or use fgets() to read the line and sscanf() to parse it, or something of the like. http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    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
    Oct 2006
    Posts
    22
    Quote Originally Posted by dwks
    How are you reading in "John Smith" from the user? I bet you're using something like this:
    Code:
    scanf("%s", name);
    scanf("%s") only reads until the first whitespace character. That is, "John" will be stored in name, and "Smith" will remain in the input stream. This could cause a problem if, for example, you read John Smith's age right afterwards; scanf() will see "Smith" and conclude that that isn't a number (which it isn't), and return an error code (which you probably don't check for), leaving the age variable unchanged.

    To read spaces into name, use fgets() instead of scanf(). http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Code:
    #include <stdio.h>  /* for BUFSIZ and fgets() */
    #include <string.h>  /* for strchr() */
    
    char name[BUFSIZ], *p;
    fgets(name, sizeof(name), stdin);
    if((p = strchr(name, '\n')) != 0) *p = 0;  /* remove the newline from name */
    When reading numbers from the user, you could discard non-digit input, or use fgets() to read the line and sscanf() to parse it, or something of the like. http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Thats exactly how I was reading in the name, but I've got it so there is an array for forename and one for surname, and two scanf statements.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Okay, but what if the user decides to enter "First Middle Last"? Or "Mr. Some One"? Or "Most Exalted High And Mighty Person"?

    Rule number one: The user is unpredictable.
    Rule number two: If something can go wrong, it will.

    [edit] If I may: http://board.theprogrammingsite.com/viewtopic.php?t=82 [/edit]
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. How to determine length of pointer array
    By Dr.Zoidburg in forum C Programming
    Replies: 12
    Last Post: 02-16-2009, 06:52 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Replies: 6
    Last Post: 02-15-2005, 11:20 PM