Thread: using array of strings in a struct

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    43

    Question using array of strings in a struct

    i have a struct that contains the number of words in a given dictionary and an array of strings. How do save and access the strings as a hole?

    this is how it is i'm my program (I'm still compiling it if possible plz only help me on the specific question, i want to figure out as much as possible)

    Code:
    struct dictionary
    {
        int size;
        char** word;
    };
    ////////////////////////////////////////////////////////////////////////////////
    // Pre-condition:   file named "dictionary.txt"
    //
    // Post-condition:  return a pointer for the struct dictionary
    //
    // Discription:     
    ////////////////////////////////////////////////////////////////////////////////
    struct dictionary* ReadDictionary(void)
    {
        struct dictionary* diction;
        char word [MAX_SIZE];
        int i;
        int word_len;
        
        FILE* fin;
        
        diction = malloc(sizeof(struct dictionary));
        fin = fopen("dictionary.txt", "r");
        
        fscanf(fin, "%d", diction->size);
        
        diction->word = (char**) malloc(diction->size * sizeof(char*));
        
        for (i = 0; i > diction->size; i++)
        {
            printf("Jumble #%d", i);
            fscanf(fin, "%s", word);
            word_len = sterlen(word) + 1;
            
            //these lines are the ones in question...mostly the second one
            //there also might be in an issue with the other malloc...
            diction->word[i] = (char*) malloc(word_len);
            strcpy(diction->word[i], word];
        }
        
        fclose(fin);
        
        return diction;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Homophones [1]: hole vs. whole?

    As to your actual question, this looks like you know already:
    Code:
            strcpy(diction->word[i], word];
    The above line copies the word from the file into the string in your array - is that what you are asking about? Or what (w)hole are you referring to?

    [1] No, not phones for gays - it means words that sound the same - and English have a lot of them: brake/break, leek/leak, lode/load, read/red and of course hole and whole.

    --
    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.

  3. #3
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    1. Where did you initialize the variable diction->size?

    2.
    Code:
    for (i = 0; i > diction->size; i++)
    this "for" will never be executed, unless the size is negative. In that case it will never stop.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by flexo87 View Post
    1. Where did you initialize the variable diction->size?

    2.
    Code:
    for (i = 0; i > diction->size; i++)
    this "for" will never be executed, unless the size is negative. In that case it will never stop.
    It will stop, but only once i reaches the value of diction size, and if we start at a very large negative number (far from zero) it may take quite some time.

    Size is initialized
    Code:
        fscanf(fin, "%d", diction->size);
    [Although the above line is missing the address-of-operator, so would likely crash].

    --
    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.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    43

    Exclamation

    the reason i asked is because this line ...

    Code:
    strcpy(diction->word[i], word]);
    causes the program to chrash. says "syntax error before ']' token"

    i have sence changed my code to

    Code:
    diction->word[i] = word;
    ...but i just realized now that wont work.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    43

    Question

    ...nm about that last one...

    the code i have now is

    Code:
    struct dictionary* ReadDictionary(void)
    {
        struct dictionary* diction;
        char word [MAX_SIZE];
        int i;
        int word_len;
        
        FILE* fin;
        
        diction = malloc(sizeof(struct dictionary));
        fin = fopen("dictionary.txt", "r");
        
        
        
        fscanf(fin, "%d", &diction->size);
        
        diction->word = (char**) malloc(diction->size * sizeof(char*));
        
        for (i = 0; i < diction->size; i++)
        {
            fscanf(fin, "%s", word);
            word_len = strlen(word) + 1;
            diction->word[i] = (char*) malloc(word_len);
            strcpy(diction->word[i], word);
        }
        
        printf("done whith the dictionary\n");
        
        fclose(fin);
        
        for (i = 0; i < diction->size; i++);
        {
            printf("%s\n", diction->word[i]);
        }   
        
        return diction;
    }
    at the print loop it prints (null) I'm probably asking for help before i need it again. but help is still welcome.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    43

    Red face

    i guess it helps if i don't close my for loop =P

    Code:
       
     for (i = 0; i < diction->size; i++);
    needs to be
    Code:
        for (i = 0; i < diction->size; i++)
    with out the ';'

    thanx for the other input to =D needa try and finish now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. memory allocation for flexible array member of struct
    By jcarouth in forum C Programming
    Replies: 3
    Last Post: 09-11-2005, 12:19 PM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM