Correct syntax for array of structs?

I can't seem to find a straight answer on this.

In my book it says to declare and array of struct like so:
1
2
3
4
5
6
7
8
9
10
11
12

struct Results {
      string words;
      int t;
};

int main() {
struct Results recResults[1000];


}
return 0;


Another question on this same topic.

when you are calling an array of struct in a function - is this correct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//declare struct

struct Results {
      string words;
      int t;
};

//declare functions
void popArray(ifstream&, string, int);

int main() {
struct Results recResults[1000];

//call function
string popArray(ifstream&, string, Results recResults[]);




}
return 0;
//function definition
string popArray(ifstream& inf, string sentence, Results recResults) {

function code


}


Thanks in advance.
Results recResults[1000];
Can you be more specific? Where does the above specifically go? What part of the code are you referring to?
Looks like the author of your book is actually a C programmer. In C++, you don't need to use the word struct every time you use one.

1
2
3
4
5
6
7
8
9
10
struct Results {
      string words;
      int t;
};

int main() 
{
  Results recResults[1000];  // NO use of word struct
}
return 0;


Your popArray function declaration has the third parameter as an int, and then you do this
string popArray(ifstream&, string, Results recResults[]);
which is NOT calling the function. It's declaring another, different function.
Then your actual definition is different AGAIN, like this:
string popArray(ifstream& inf, string sentence, Results recResults)
What do you want the third parameter to be?
Apologies -

it should read:

1
2
3
4
5
6
7
8
9
10
11
12
13

//function declaration
string popArray(ifstream&, string, Results recResults[]);

//function call
 popArray(inf, sentence, recResults);

//function definition
string popArray(ifstream& inf, string sentence, Results recResults) {

function code

}


Please tell me if all of these have correct syntax?

Thanks.
Last edited on
The declaration and the definition disagree over what type the third parameter is.
1
2
3
4
5
string popArray(ifstream& inf, string sentence, Results recResults) {

function code

}

I thought you were passing in an array of Results? This function only takes one Results struct as an argument, not an array of them.
Last edited on
That is the problem I'm affraid - I am new to structs/array of structs and as you can tell a bit confused. I am trying to use an array of structs and have never done this before - thus my question about syntax.

I am trying to pass struct members to an array of structs. In fact, in this particular function I am trying to populate the array with the struct member information.

At any rate, I keep wondering if I am using the syntax correctly because it would help if I know that I am at least calling these things correctly.

Does that help describe what I am trying to do?

Have you used arrays? Using an array of int is identical to using an array of some struct.
@xhtmlx DUH DUH DUH. I get it now. Thank you for pointing out the painfully obvious - I fixed my code. Thanks again.
Topic archived. No new replies allowed.