1

I don't understand why I got an error when I try to initialize a struct in such a way

typedef struct _floor
{
   int room;
   int height;
   int room_dim[room][2];
}Floor;

Why can't I use room to initialize room_dim array?

Mazzy
  • 13,354
  • 43
  • 126
  • 207

3 Answers3

5

A struct must have a size that is known at compile-time. room is a struct variable, and could have any value; therefore, it is not a compile-time constant and cannot be used to size a struct member.

Instead, you can make the final element a flexible array member and allocate it at runtime:

struct floor {
    int rooms;
    int height;
    int room_dim[][2];
};

struct floor *make_empty_floor(int rooms) {
    struct floor *ret = malloc(sizeof(struct floor) + sizeof(ret->room_dim)*rooms);
    ret->rooms = rooms;
    return ret;
}

Now you can use ret->room_dim as usual, and the extra malloc'd space will be used for room_dim.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • An array can't have zero length. That's a non-standard GCC extension. – Lundin Aug 06 '13 at 11:37
  • Whoops, I meant to use a flexible array member, not a zero-length array member. – nneonneo Aug 06 '13 at 11:38
  • No it is not. [See this](http://stackoverflow.com/questions/9722632/what-happens-if-i-define-a-0-size-array-in-c-c/9723093#9723093). – Lundin Aug 06 '13 at 11:39
  • 1
    Flexible array members first existed in pre C99 versions of GCC, where GCC allowed deterministic behavior if the last struct member was declared as an array of length zero. Then in C99, flexible array members were added to the standard, but they should be written as in your edited example, with [] rather than [0]. The [0] was always a non-standard GCC extension. [More info here](http://stackoverflow.com/questions/17344745/how-to-use-flexible-array-in-c-to-keep-several-values/17345123#17345123). – Lundin Aug 06 '13 at 11:44
  • Instead of `sizeof(int)*2*rooms`, I would write `sizeof(ret->room_dim[0])*rooms`. If I change the type of `room_dim`, this adapts itself seamlessly. – glglgl Aug 06 '13 at 11:44
  • 1
    @Lundin: Thanks. I knew the right syntax, but my fingers decided to type the wrong thing anyway (since I used to program for GCC more often than C99). – nneonneo Aug 06 '13 at 11:46
1

You are trying to dynamically initialize an array. This means you will only know the size of room at run time.

[Edited after comments]

To understand it simply, without use of struct, even this won't compile below C99, however in structs this is still not allowed

int n;  
int size[n];
user2618142
  • 1,035
  • 2
  • 18
  • 35
  • 1
    Well, actually, in C99, VLA's are legal. Thus, in C99, your code snippet will compile. VLAs are still prohibited in structs. – nneonneo Aug 06 '13 at 11:28
  • user26XXX please have a look [here](http://stackoverflow.com/questions/14629504/variable-length-array-in-the-middle-of-struct-why-this-c-code-is-valid-for-gcc) – Dayal rai Aug 06 '13 at 11:32
1

you can instead use malloc to get the dynamic memory rather then using room_dim[room][2] .Since the above is not the allowed standard in c compiler.

for example

typedef struct floor
{
   int *room_dim[];
   int height;
   int room;
}floor;
scanf("%d",&room);
floor.room_dim=(floor *) malloc(sizeof(floor)*room);
Madan Ram
  • 880
  • 1
  • 11
  • 18