3

I have a 2d array of structs like this,

MapStruct myMap[50][50];

So we can initialize like this,

myMap[0][0].left = 0;
myMap[0][0].up = 1;
myMap[0][0].right = 5;

I know that I can also use the below example,

MapStruct myMap[50][50] = { {0,1,5}, {2,3,7}, {9,11,8} ... };

But the problem is that there are significant empty spots in this 50x50 structure. So for example maybe from [30][40] up to [40][50] is empty and some other points here and there are empty so with the above bracket notation i have to leave empty brackets like this {},{},{} for those empty spots.

Now my question is is there a way to initialize the like below?

myMap[0][0] = {0, 1, 5}; // gives a syntax error

Saves two lines per point I'd be pretty happy with that.

Ps: I'm using the indices myMap[x][y] as keys like a dictionary object so I can't just get rid of those empty ones in the middle because that changes the indices.

Bob
  • 31
  • 1
  • 2

5 Answers5

4

C99 allows

myMap[0][0] = (MapStruct){0, 1, 5};

If you are restricted to C90, you can use an helper function.

mypMap[4][2] = makeStruct(3, 6, 9);

But note that

MapStruct myMap[50][50];

in a function won't initialize the array with 0 values if there are no initializer, so you'll have to use

MapStruct myMap[50][50] = {0};

And also note that one may wonder if it is wize to allocate such big arrays on the stack.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
  • thanks for your answer, Considering that the points cannot be generated on the fly and are constants, and that I need to access them with 2d indices, is there any better way? – Bob May 13 '11 at 15:21
  • `{}` is not permitted. You got to give it at least one little `0` inside. `{ 0 }` is the default initializer for any type in C99. – Jens Gustedt May 13 '11 at 15:35
  • @Jens, fixed, thanks for noticing. For what is worth, an empty initializer is valid C++. – AProgrammer May 13 '11 at 18:20
1

If you try with :

myMap[0][0] = (MapStruct) {.left = 0, .up = 1, .right = 5};

Or

myMap[0][0] = (MapStruct) {0, 1, 5};
Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • I bet that is not correct C syntax, but I'm ready to get convinced of the opposite. – Christian Rau May 13 '11 at 14:34
  • @Christian: [convince](http://en.wikipedia.org/wiki/C_syntax#Initialization) Also ISO C (9899) §6.7.8 – nmichaels May 13 '11 at 14:38
  • Ok, convinced, didn't know that. The cast is neccessary, as now it is not an initialization (which only works in the declaration) but an assignment of a temporary variable. You create a new onject on the stack (and initialize it) and copy its data to myMap[0][0]. – Christian Rau May 13 '11 at 15:03
  • @Cedric, it's not a cast, it is a part of the compound literal syntax. – u0b34a0f6ae Dec 08 '11 at 14:08
1

C99 allows initialization like this:

MapStruct myMap[50][50] = {
    [ 0][ 5] = { /* ... */ },
    [10][20] = { /* ... */ },
    /* ... */
};

Or, you can set up the values by assignment, like this:

MapStruct myMap[50][50];
/* ... */
myMap[ 0][ 5] = (MapStruct){ /* ... */ };
myMap[10][20] = (MapStruct){ /* ... */ };
/* ... */

Be aware that the syntax in the second method is not casting. It is notation introduced by C99 that, although it looks the same as a cast, is used to write literals of aggregate types.

Anonymous
  • 11
  • 1
0

If you're using C99, look up compound literal.

Nyan
  • 2,360
  • 3
  • 25
  • 38
0

This will give you data on the heap rather than the stack but what you want to do could be achieved with calloc from <stdlib.h>. calloc will allocate the memory space and it set initialize it to zero.

MyStruct * myMap = calloc( 50, 50 * sizeof(MyStruct));
zellio
  • 31,308
  • 1
  • 42
  • 61