Thread: arrays of structs

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    13

    arrays of structs

    hi there
    i'm new to C programming, and there's this code i'm trying to get running

    Code:
    typedef struct
    {
    	char word[];
    } dictionary;
    
    dictionary dict[749];
    dict[0].word[] = "account";
    dict[1].word[] = "act";
    dict[2].word[] = "addition";
    dict[3].word[] = "adjustment";
    
    //...
    
    dict[748].word[] = "wrong";
    this code is in a header file, and it's linked with my main program. however, it won't compile, saying there should be "=" or "," or ";" etc. before the "." token...? what does this mean?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Should get an notification for a zero sized array. Should be

    Code:
    typedef struct
    {
    	char * word;
    } dictionary;
    
    dictionary dict[749];
    dict[0].word = "account";
    dict[1].word = "act";
    dict[2].word = "addition";
    dict[3].word = "adjustment";
    
    //...
    
    dict[748].word = "wrong";
    Or

    Code:
    typedef struct
    {
    	char word[32];
    } dictionary;
    
    dictionary dict[749];
    strncpy(dict[0].word, "account", sizeof(dict.word[0]));
    strncpy(dict[1].word, "act", sizeof(dict.word[1]));
    strncpy(dict[2].word, "addition", sizeof(dict.word[2]));
    strncpy(dict[3].word, "adjustment", sizeof(dict.word[3]));
    
    // ...
    Or something..

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    why not start off with something a bit simpler?

    I have been programming for years, but I started at the shallow end!!

    And I would never attempt anything as monsterous as that today!!

    But then I like coding not reading books!!
    Last edited by esbo; 09-09-2006 at 11:39 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    esbo: i have done some other programming languages before, including C++, but i've never tried arrays of structs or classes.

    tonto:

    the first alternative, it gives the same error message.
    the second alternative, it says "expected ')' before '[' token"... does this mean there's something wrong with the array of structs itself?

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Cat
    Here's a far better way of doing things:

    Code:
    std::vector<std::string> dictionary;
    dictionary.reserve(749); // This just cuts down on the number of resizes, it's not strictly needed
    
    dictionary.push_back("account");
    dictionary.push_back("act");
    dictionary.push_back("addition");
    dictionary.push_back("adjustment");
    *cough* C Programming Board *cough*
    Sent from my iPadŽ

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    First alternative: Do it in a function, not globally.
    Second alternative: I meant sizeof(dict[indice].word), not sizeof(dict.wird[indice])

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    thanks for all your help, guys. i've managed to get it going =D

    i was doing it globally, and putting it in a function made it work

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    sorry for the double post, i really need help

    i've got a random number, and i want to pass it to the getWord function to get the random word, but the compiler says something about "return makes integer from pointer without a cast". but i want it to return a string! how do i do that?

    Code:
    typedef struct
    {
    	char * word;
    } dictionary;
    
    //declaration of function
    char getWord(int);
    
    char getWord(int wordIndex)
    {
    dictionary dict[749];
    dict[0].word = "account";
    dict[1].word = "act";
    dict[2].word = "addition";
    dict[3].word = "adjustment";
    
    //...
    
    dict[748].word = "wrong";
    
    return dict[wordIndex].word;
    }

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You could either return a character pointer or, more appropriately I'd say, you could pass a char* to the function to be copied into as the strcpy(), strcat() functions do. It really depends on if your more likely going to store the returned string or just print it out.
    Last edited by SlyMaelstrom; 09-10-2006 at 12:37 AM.
    Sent from my iPadŽ

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    It's okay how he has it, because the strings are in static memory, but if it was local automatic memory, that would be more appropriate.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Tonto
    It's okay how he has it, because the strings are in static memory, but if it was local automatic memory, that would be more appropriate.
    Well, if the function looks anything like the example he posted, then it appears to be local to the function. He'd have to make that struct static, I believe.
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    could you please explain that with an example? thanks.

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Why can't you just use something like this:
    Code:
    char* getWord(int wordIndex){
    	static char dict[749][11]={"account","act","additon","adjustment","bowwow","bowwow", /* 0-5 */
    		"bowwow","bowwow","bowwow","bowwow","bowwow","bowwow","bowwow","bowwow", /* 6-13 */};
    	return dict[wordIndex];
    }
    
    #include <iostream.h>
    using namespace std;
    int main(){
    	cout<<getWord(1);
    	cin.get();
    	return 0;
    }
    The only problem with it is that when you have some words missing from the middle, you'd have to do many ,"" things
    ...and it's quite messy...
    Last edited by maxorator; 09-10-2006 at 01:04 AM.

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    char* getWord(int randNum) {
       static dictionary dict[5];   // Make sure this is static, otherwise you'll be returning a local variable
       
       /* I would also put a counter in a conditional around all of these assignments
           because it's a whole lot of unnessasary copying everytime you call the function */
       strcpy(dict[0].word, "Hello");
       strcpy(dict[1].word, "World");
       strcpy(dict[2].word, "How");
       strcpy(dict[3].word, "are");
       strcpy(dict[4].word, "you");
       
       return dict[randNum].word;
    }
    Last edited by SlyMaelstrom; 09-10-2006 at 12:52 AM.
    Sent from my iPadŽ

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    thanks heaps =D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allocatable arrays in CLASSes and STRUCTS
    By simone.marras in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2009, 10:50 AM
  2. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM
  3. Using strcpy() and arrays of structs
    By vital101 in forum C Programming
    Replies: 3
    Last Post: 04-26-2007, 09:04 PM
  4. arrays in structs
    By *DEAD* in forum C Programming
    Replies: 3
    Last Post: 11-25-2006, 07:35 AM
  5. malloc for structs and arrays
    By rkooij in forum C Programming
    Replies: 15
    Last Post: 05-04-2006, 07:38 AM