Thread: Array of Strings help

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    25

    Array of Strings help

    Hey guys, I am stuck with another C programming problem that I can't seem to get around.

    The problem I am having is trying to read a file of words, like a story, into an array of strings. What I've gathered so far is that you can use a char pointer with each spot being a unique string or word. But what I can't seem to figure out is how to get each word from the file into its own spot in the array. The maximum number of words is 1000 and the maximum number of characters per word is 80, but that must be allocated using malloc().

    So far, I have the pointer defined as:
    Code:
    char *words[1000];
    Which, to my understanding, should allow me to do something like:
    Code:
    words[0] = "hello";
    words[1] = "world!";
    Which should then store Hello in the first spot of words. Am I correct in this thinking? If so, then I can't figure out a way to pull a word out of the file one by one.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    To read input (words) from a file and store them in your array, as you have it written currently, you'd need to allocate memory for each spot in the array (with malloc). Simple direct assignment of some strings such as "hello" and "world!" work because the program is going to assign the address of the string literals to the pointers in the array. Reading data from a file however is different and you'd need to read into a temporary buffer of some sort and calculate its size/length, then allocate the space based on this length, copy the word over to this space, and assign this allocated space's address to the memory slot at the current index in the array. Even then, using an array you're stuck with a file that has at most 1000 words (which I guess isn't an issue with you).

    Code:
    const int MAXWORDSIZE = 80;
    char tempword[MAXWORDSIZE+1]; /* +1 for the null */
    int index = 0;
    
    loop through file
        read a word from file into tempword;
        calculate and save length of word in tempword;
        malloc memory based on length of tempword (+1 for NULL);
        copy from tempword into this newly allocated chunk of memory (including null);
        assign words[index] to this allocated memory chunk;
        ++index;
    Last edited by hk_mp5kpdw; 09-20-2007 at 11:29 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    Quote Originally Posted by hk_mp5kpdw View Post
    To read input (words) from a file and store them in your array, as you have it written currently, you'd need to allocate memory for each spot in the array (with malloc). Simple direct assignment of some strings such as "hello" and "world!" work because the program is going to assign the address of the string literals to the pointers in the array. Reading data from a file however is different and you'd need to read into a temporary buffer of some sort and calculate its size/length, then allocate the space based on this length, copy the word over to this space, and assign this allocated space's address to the memory slot at the current index in the array. Even then, using an array you're stuck with a file that has at most 1000 words (which I guess isn't an issue with you).

    Code:
    const int MAXWORDSIZE = 80;
    char tempword[MAXWORDSIZE+1]; /* +1 for the null */
    int index = 0;
    
    loop through file
        read a word from file into tempword;
        calculate and save length of word in tempword;
        malloc memory based on length of tempword (+1 for NULL);
        copy from tempword into this newly allocated chunk of memory (including null);
        assign words[index] to this allocated memory chunk;
        ++index;
    Hey, thanks for the help! It's funny though, because we figured out what we had to do about 2 minutes before you posted it! I really appreciate the help though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM