0

I'm trying to allow a variable length array inside a struct in a C program. Something like this:

struct result{
    int column;
    int row;
    const char* a[var][var];  
};

how do i do this?

even the following definition would do:

struct result{
    int column;
    int row;
    const char* a[row][column];
};

plese do help...

Prasanth Madhavan
  • 12,657
  • 15
  • 62
  • 94

4 Answers4

2

You can't have variable size arrays in C structs.

You can have pointers to arrays which can be variable size (you need to handle the allocation of the space separately), but you are declaring arrays of pointers instead

If you want an array of pointers, try

const char (*a)[][];

(you'll need to manage the array as an array of pointers to arrays if you want both dimensions to be variable)

The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
  • Actually you can have variable size arrays in structs and they can be very useful, despite their many limitations. – thkala Dec 08 '10 at 08:53
  • @thkala, that appears to be a Microsoft extension, not standard C: http://msdn.microsoft.com/en-us/library/b6fae073%28VS.71%29.aspx – The Archetypal Paul Dec 08 '10 at 09:01
  • GCC supports it as well... It's part of at least the C99 standard (paragraph 6.7.2.1, sub-paragraph 16) – thkala Dec 08 '10 at 09:25
  • @thkala: technically there is a difference between a VLA and a flexible array member. If VLAs would be allowed in structures there would be no syntactical way to refer to the type of an instance, e.g in a declaration. Structures with flexible array members can only be used through pointers and you must allocate storage through `malloc`. – Jens Gustedt Dec 08 '10 at 09:55
  • @Jens: true, they are not VLAs and they are not _that_ flexible either. A programmer could do the same thing by overallocating space for the struct and accessing the space past its end directly, although there would be alignment and type information issues. – thkala Dec 08 '10 at 10:14
2

Just use pointers instead. You'll have to do dynamic memory allocation. Don't forget to free() the memory allocated for your array :)

P.S.: If you need a 2-dimensional array, use a pointer-to-pointer (that is, allocate memory for an array of pointers)

1

Use dynamic allocation with the malloc function.

In your case you should do something like:

#include <stdlib.h> /* header file for the malloc function */

void allocateResult(struct result* res, int row, int column) {
    res->a = (const char*) malloc(row * column * sizeof(char));
}

Please note that sizeof(char) equals to 1 (almost all the time), but for other types you'd have to do it this way.

This solution implies to free allocated memory before the program ends. You'll have to pass the pointer in your struct to free:

void freeResult(struct result* res) {
   free(res->a);
}
jopasserat
  • 5,721
  • 4
  • 31
  • 50
1

For single-dimension arrays you can do something like this:

struct TEST {
    ...
    int size;
    char string[];
}

where the size field indicates how many characters there are in the string array. The array has to be the last member of the struct, and you have to allocate the struct's memory dynamically. The allocated size should be sizeof(struct TEST) + size * sizeof(char) in this case.

You cannot have more than one variable size array in the struct. Multi-dimension variable-size arrays are trickier. It cannot be done unless only one dimension size is unknown, specifically that of the first dimension.

struct TEST {
    ...
    int size;
    char string[][100];
}

EDIT:

As other posters mentioned, you can have pointers to one or more arrays, at the cost of having to manage their memory areas separately from the struct.

EDIT 2:

This is part of at least the ISO C99 standard. Shamelessly copying from paragraph 6.7.2.1, sub-paragraph 16:

16 As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. With two exceptions, the flexible array member is ignored. First, the size of the structure shall be equal to the offset of the last element of an otherwise identical structure that replaces the flexible array member with an array of unspecified length.106)...

thkala
  • 84,049
  • 23
  • 157
  • 201