-2

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    `cases[0].ques[5]={-7,-6,-5,-4,-3};` -- Where did you get the idea that that is valid C? Even if it were, it would be trying to cram 5 values into ques[5] ... when only ques[0] ... ques[4] were allocated. – Jim Balter Apr 10 '14 at 06:51
  • @JimBalter : I will say size of `ques` is `cases[counter].len` which may or may not be `5`. – 0xF1 Apr 10 '14 at 06:55
  • 1
    As always: do not cast the pointer, returned by `malloc`. – Elias Van Ootegem Apr 10 '14 at 07:00
  • @MadHatter Eh? The OP wrote `cases[0].len=5;`, so it's 5. (And "size of" should refer to the size in bytes, i.e., `sizeof`) – Jim Balter Apr 10 '14 at 07:03
  • @JimBalter: What I meant was `cases[0].ques=(int *)malloc(cases[counter].len*sizeof(int));` `counter` is not known and hence space allocated to `ques`. Anyways, I think it should be `0` in this case, so you were right the first time. – 0xF1 Apr 10 '14 at 07:43
  • @MadHatter Ah, ok ... presumably OP meant `cases[counter].ques=(int *)malloc(cases[counter].len*sizeof(int));` – Jim Balter Apr 10 '14 at 17:46

7 Answers7

4

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.

0xF1
  • 6,046
  • 2
  • 27
  • 50
  • Thank you sir. Is there a way to initialize an array which has many integers, say about 50? I have to give the values statically so i dont think i can use a for loop. – sandeep srivastav vaddiparthy Apr 10 '14 at 06:46
  • @user3153983 MadHatter's update mentions `memcpy`, which is the most sensible answer. If you find this answer helpful, you should accept it. – Jim Balter Apr 10 '14 at 06:54
4

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.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
3

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.

LearningC
  • 3,182
  • 1
  • 12
  • 19
  • Thank you sir/madam. Is there a way to initialize an array which has many integers, say about 50? I have to give the values statically so i dont think i can use a for loop. and the method mentioned above seems tedious. – sandeep srivastav vaddiparthy Apr 10 '14 at 06:46
  • @user3153983 how you get those 50 values? any logic? – LearningC Apr 10 '14 at 06:48
  • @user3153983 ok. how do you get those 50 values? from user input or how you get those values? – LearningC Apr 10 '14 at 06:53
  • Dear all, my array has to have values that are totally random. This is to test a program. So i guess i have to assign each individual element in the array. – sandeep srivastav vaddiparthy Apr 10 '14 at 06:58
  • @user3153983 you can add the code to get the random number inside a loop and assign each number as you get to cases[0].ques[i]. isnt it possible to use this technique for your program? – LearningC Apr 10 '14 at 07:03
1

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

phuclv
  • 37,963
  • 15
  • 156
  • 475
1

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. :)

Kraken
  • 23,393
  • 37
  • 102
  • 162
0
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;
}
Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
0

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));
Community
  • 1
  • 1
adripanico
  • 1,026
  • 14
  • 32