Assign values to array of structures

This is a discussion on Assign values to array of structures within the C++ Programming forums, part of the General Programming Boards category; This should be a pretty simple question but I've searched all over and can't find what I'm looking for. I ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    20

    Assign values to array of structures

    This should be a pretty simple question but I've searched all over and can't find what I'm looking for. I have an array of structs, each struct has an integer, string, and pointer to another struct inside it.

    Code:
    typedef struct node_ {
    int count;
    string w;
    node *next;
    } node;
    I initialized an array of 100000 nodes.

    Code:
    const in size = 1000000;
    node array[size];
    Now, say I wanted to assign the integer in struct a value, in array item 745 (random number). How would I do this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Generally, we use "=" for our assigning needs. You put the value on the right, and where you want it to go on the left.
    Code:
    array[wherever].count = something;

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    Quote Originally Posted by tabstop View Post
    Generally, we use "=" for our assigning needs. You put the value on the right, and where you want it to go on the left.
    Code:
    array[wherever].count = something;
    Yea, this format is giving me an error. What about assigning the string a value? Same procedure?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You use strcpy to assign to a (C-style) string. If it's a std::string, then = works just marvelously.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    My assigning still is not working properly, allow me to post some more code;

    Code:
    const int size=1000000;
    
    struct node
    {
    int count;
    string w;
    
    } table [size];
    Right here I want to create an array of 1000000 nodes called table.

    Code:
    do
    {
            word="";
            do
            {
            c=fgetc(fp);
            if (c >='a' && c<='z')
                    c=c-32;
            ch=char(c);
            word+=ch;
            } while ((c>='A' && c<='Z') || c==39);
    
            hash=hashfunction(word);
                    if (table[hash].w == "")
                    {
                    table[hash].w=word;
                    table[hash].count=1;
                    }
                    if (table[hash].w==word)
                    {
                    table[hash].count+=1;
                    }
    
    } while (c!=EOF);
    This segment of code is supposed to read in characters from a file pointer and first convert them to upper case, if applicable. Then pass the upper case string through a hash function which returns an int (the location of the struct in the array), and then assign that upper case word to string variable inside my struct. Just a basic word counter program here.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are you getting a compile error or a runtime error? I think you'll get lucky to create a 1000000-element array.

    But other than me not having a function hashfunction, all the code you posted compiles marvelously.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    There are no errors at this point, its just when I go to retrieve the data from the array of structs there is nothing. It prints nothing, and I can post the hashfunction if you think that would be helpful.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're certainly adding things to your table array. I assume it's passed in/global. I'm pretty sure you can get away with assuming that a string that's not been assigned to is equal to "", so that seems okay. I don't see anything wrong yet.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    Quote Originally Posted by tabstop View Post
    You're certainly adding things to your table array. I assume it's passed in/global. I'm pretty sure you can get away with assuming that a string that's not been assigned to is equal to "", so that seems okay. I don't see anything wrong yet.
    The problem was elsewhere in my code, it all works now, thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making A Program That Outputs Array Values
    By xxxixpats in forum C Programming
    Replies: 3
    Last Post: 11-04-2010, 06:21 PM
  2. Making A Program That Outputs Array Values
    By xxxixpats in forum C Programming
    Replies: 1
    Last Post: 11-03-2010, 11:08 AM
  3. Replies: 12
    Last Post: 09-23-2010, 12:49 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. 2 Dimenstional Array vs Array of Structures
    By jrahhali in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2004, 05:51 AM

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