0

Why isn't it in C (or C++) allowed?

I mean this:

ucell GetPlayerPosPositionData[5];
GetPlayerPosPositionData = {4*sizeof(cell),playerid,0,sizeof(cell),2*sizeof(cell)};//this does not work, error

//normal assignment
GetPlayerPosPositionData[1] = playerid;
GetPlayerPosPositionData[2] = 0;
GetPlayerPosPositionData[3] = sizeof(cell);
GetPlayerPosPositionData[4] = 2*sizeof(cell);

4 Answers4

2

Because arrays are not assignable. You can initialize them at the point of declaration or you can assign them on a per-element basis, but that's it. This would work:

ucell GetPlayerPosPositionData[] = {4*sizeof(cell),
                                    playerid,
                                    0,
                                    sizeof(cell),
                                    2*sizeof(cell)};
Ed S.
  • 122,712
  • 22
  • 185
  • 265
1

Because the syntax says it is not allowed? There are two parts to the problem:

  1. You can't use the initializer syntax except in an initializer or (in C99) in a compound literal.

  2. You can't assign whole arrays.

In C99, you could pass an array to a function:

typedef unsigned int cell;

extern void some_func(cell data[]);

extern void func(void);

void func(void)
{
    cell playerid = 0;
    some_func((cell[]){4*sizeof(cell),playerid,0,sizeof(cell),2*sizeof(cell)});
}

(Your question is unclear on cell vs ucell; I chose to use cell throughout.)

You still can't do array assignment. If the array is wrapped in a structure, you can do the structure assignment which, coincidentally, assigns the contained array. But that is a structure assignment, not an array assignment.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • You're not actually passing an array to `some_func`. The function receives a pointer to the first element of `data`, regardless of the argument notation. – Ed S. Dec 08 '12 at 20:55
  • The literal array decays to a pointer to the first element when it is passed to the function — yes, of course, just like any other array would decay into a pointer to the first element. But the compound literal notation has created a literal array. – Jonathan Leffler Dec 08 '12 at 20:57
  • Yes, of course. I suppose I'm a bit hypersensitive to arrays and their semantics because so few people really understand the difference (I realize that you do, the comment was for the OP and others). – Ed S. Dec 08 '12 at 20:59
  • Arrays and pointers are a source of confusion. Another way of looking at it would be with `extern void another_func(cell (*data)[5]);`, a function which takes a pointer to an array of 5 `cell` values. I could invoke: `another_func(&(cell[]){4*sizeof(cell),playerid,0,sizeof(cell),2*sizeof(cell)});` which would not work if the compound literal was not an actual array. – Jonathan Leffler Dec 08 '12 at 21:04
  • Yep. Pointers to arrays are even less well understood. – Ed S. Dec 08 '12 at 21:05
0

That syntax is for list-initializing an array. It can only be used as the initializer of your array declaration:

ucell GetPlayerPosPositionData[5] = {4*sizeof(cell),playerid,0,sizeof(cell),2*sizeof(cell)};

The alternative is to individually assign to each of the elements:

ucell GetPlayerPosPositionData[5];
GetPlayerPosPositionData[0] = 4 * sizeof(cell);
GetPlayerPosPositionData[1] = playerid;
GetPlayerPosPositionData[2] = 0;
GetPlayerPosPositionData[3] = sizeof(cell);
GetPlayerPosPositionData[4] = 2 * sizeof(cell);
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

I will let the language lawyers argue over this. Suppose array is declared as int array[4], and you wish to assign to it. Then consider:

* (struct t { int array[4]; } *) array = (struct t) {{ 0, 1, 2, 3 }};
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312