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

1
  • Whats the trouble you are having ? Commented Dec 8, 2010 at 8:39

4 Answers 4

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)

Sign up to request clarification or add additional context in comments.

5 Comments

Actually you can have variable size arrays in structs and they can be very useful, despite their many limitations.
@thkala, that appears to be a Microsoft extension, not standard C: msdn.microsoft.com/en-us/library/b6fae073%28VS.71%29.aspx
GCC supports it as well... It's part of at least the C99 standard (paragraph 6.7.2.1, sub-paragraph 16)
@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: 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.
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)

Comments

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

Comments

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);
}

4 Comments

can u give me an example.. as i am havin a hard time visualizing the thing.
Here it is, don't hesitate to comment again if you need more details.
sizeof(char) equals to 1 almost all the time. Not absolutely all the time.
it's always 1 in c99 as said in (stackoverflow.com/questions/2215445/…) but is's a good point to use sizeof nonetheless

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.