# Thread: Assign values to array of structures

1. ## 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. 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. Originally Posted by tabstop 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. You use strcpy to assign to a (C-style) string. If it's a std::string, then = works just marvelously. 5. 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. 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. 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. 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. Originally Posted by tabstop 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 