Strings inside structs: How to set it?

Hi!

Why i'm getting the following error message when i try to set a sentence for Question and how can i fix that? I'm using 2d array because i want to organize the questions in different categories.

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
30
31
#include <stdio.h>
#define MAX 6
#define LENGTH 255
 
typedef struct {
    char sentence[LENGTH];
    char x;
}Question;
 
void setQuiz( Question quiz[MAX][MAX] );

int main( ) {
    Question quiz[MAX][MAX];

    setQuiz( quiz );
 
    return 0;
}



void setQuiz( Question quiz[MAX][MAX] ) {

	quiz[MAX][MAX] = {//Error message here.
	{"question01", 'c'},
	{"question02", 'a'},
	{"question03", 'f'},
	{"question04", 'g'},
	{"question05", 'k'}};

}


Error message:
test.c: In function ‘setQuiz’:
test.c:24:19: error: expected expression before ‘{’ token
Last edited on
I am not sure what you want to do in the setQuiz function. On line 24 quiz[MAX][MAX] is a single element of the quiz array, which also happens to be out of bounds. You can go up to MAX-1, since you start from 0.In addition, you cannot initialize quiz on line 24, only on line 13. The only way is to explicitly set each element of the array, with a for loop.
Topic archived. No new replies allowed.