简体   繁体   中英

Syntax Error : '{'

I am writing a C program that uses an array of integers which is a part of a structure. The structure is:

struct test
{
    int *ques;
    int a;
    int b;
    int len; // len is the length of the array defined in the structure as ques.

};

In a function after this declaration I have assigned a value to the len as:

cases[0].len=5; // here cases is an array of the type struct test.

and then I have used malloc to allocate memory to the array member ques as follows:

cases[0].ques=(int *)malloc(cases[counter].len*sizeof(int));

After that I have tried to fill in the array ques as follows:

cases[0].ques[5]={-7,-6,-5,-4,-3};

and while compiling I get an error at the above line stating that:

maxmin.c(47) : error C2059: syntax error : '{'

Can you please help me out?

This is invalid: cases[0].ques[5]={-7,-6,-5,-4,-3};

You can initialize an array in C this way, only at the time of declaration of array.

To fill in the values at this point in your C program you should use for loop to fill every index separately.

Right now the C compiler parses this statement as:

Value of index 5 of array ques at index 0 of structure array cases of type struct test is {-7,-6,-5,-4,-3} , which is certainly invalid since you cannot use { while assigning a value to a variable.

Update at OP's comment:

You can keep all the values in temp array say int temp[50] = {...}; then after allocating space to ques you can use memcpy function to copy len number of values to ques .

If you have C99 or C11, you can use a compound literal in conjunction with memcpy() or memmove() to initialize the new array:

memmove(&cases[0].ques, (int []){-7,-6,-5,-4,-3}, 5 * sizeof(int));

This also fixes the problem that you allocate cases[0].ques as a pointer to 5 integers, but you then try to assign to a single element that is beyond the end of the allocated array. The code is also inconsistent in that most of it references cases[0] but the malloc() call references cases[count] in the argument list. Not automatically wrong, but definitely unusual.

The array literal can be as big as you need. The only tricky bit is the size argument (third argument) to memmove() . I ducked fast on that; repeating the compound literal doesn't feel like the right move, but manually counting isn't a good solution either.

Well, you could if memmove() wasn't a macro that objected to being given 7 arguments instead of 3 (GCC 4.8.2 on Mac OS X 10.9.2)!

(memmove)(&cases[0].ques, (int []){-7,-6,-5,-4,-3}, 5 * sizeof(int));
memmove(&cases[0].ques, ((int []){-7,-6,-5,-4,-3}), 5 * sizeof(int));

The first invokes the function memmove() , not a macro. The second creates a single argument by wrapping parentheses around the compound literal. Both compile.

Note that although the compound literal looks like a cast and a brace-initializer, I'm not sure it is strictly a cast — it is best not to think of it as a cast but as a compound literal.

Incidentally, if the objective is to add N random values to an array of N integers (as mentioned in a comment to one of the other answers), then you won't want to use this technique — the integers in the example are implausible as a random sequence. If you do want 50 random numbers in some range, you'll use a random number generator (possibly rand() , possibly lrand48() , possibly something else) to create the values in an appropriate range, using a simple for loop to initialize the correct number of array elements.

assign value to each array element as,

cases[0].ques[0]=-7
cases[0].ques[1]=-6
...
cases[0].ques[5]=-3

since you are assigning the value after declaration. C allows {} use for initializing array at the time of declaration. not after that.

ques[5] is an int , you cannot assign an array to it

Also, C doesn't allow to assignment to multiple elements in an array like that. You can only do it at initialization . So create a new array and initialize with those 5 elements and copy it to your destination with memcpy() or memmove()

int[] tmp = {-7,-6,-5,-4,-3};
memcpy(cases[0].ques, tmp, 5*sizeof(int));

Alternatively you can assign values to ques[i] separately

I hope you realize why cases[0].ques[5] is wrong.

int arr[5] = {1,2,3,4,5};

The above code works because you are doing the initialization of array at the declaration time.

If you have

int arr[5];
arr[5] = {1,2,3,4,5}; // THIS WONT WORK

Here, you'll have to use for loop and fill each index.

also, when you do

int arr[5];
arr[5] = {1,2,3,4,5}; 

In the above code, what you are trying to do is, put some value in arr[5]. That is just one index of the entire array.

Additionally, as the comment suggests, arr[5] is the 6th element in the array of size 5. Now although it won't give error, but be prepared to get some unexpected results. :)

cases[0].ques[5]={-7,-6,-5,-4,-3};

You cannot do this, this initializer only valid when you define an array.

You should do something like:

for (start = -7, i = 0; i < cases[0].len; i++, start++) {
    cases[0].ques[i] = start;
}

Lot of time I do not program in C, but I think the only situation in which you can do a complete array declaration is in the time that you declare it:

int a[] = {...}

But not this way:

int a[5];
a = {...};

Also, in your case, you are doing

int a[5] = {...}

and this means that you are trying to assign an array to an integer.

Also, it's better if you do not do a cast in the malloc call :

cases[0].ques = malloc(cases[counter].len * sizeof(int));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM