12

I've created 2 structures to represent images (a pixel and an image) in C.

typedef struct pixel {
    unsigned char red;
    unsigned char green;
    unsigned char blue;
};

typedef struct image {
    int width;
    int height;
    struct pixel pixels[width][height];
    };

I am getting an error saying that width and height are not defined in the definition of the image structure. I don't understand why I'm getting that error or how I can solve it?

3
  • 2
    C99 does not allow VLA appears in struct. the reason is simple: if the field of struct is VLA, how could i decide how large this struct is? Commented Oct 3, 2014 at 17:29
  • Yep, C needs to know the array size at compile time. Images in C are usually represented by a pointer to the data that's malloc'ed once the height and width are known. Commented Oct 3, 2014 at 17:32
  • I know this is an old post, but depending on your situation you can achieve this to a degree in portable C without dealing with the no array or parent object requirements for variable length array members in C23. Say your variable array is an int array, and you are allocating an array of these structs. Just put an int at the end of the struct, and use its address as the address for an int array, that at time of array fill is alloted space after the array. If you watch your padding and alignment, this can be a standard compliant solution in some cases. Commented Oct 5, 2024 at 23:33

3 Answers 3

13

In C99 and later, you can have a (one-dimensional) flexible array member (FAM) at the end of a structure:

§6.7.2.1 Structure and union specifiers

¶18 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. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

That means you could write:

typedef struct image
{
    int width;
    int height;
    struct pixel pixels[];
} image;

but you would have to do the 2D-to-1D index mapping yourself. You also have to be careful how you allocate the structure (of necessity, it will be allocated by malloc() as otherwise the size of the array will be zero).

Note the addition of a name for the typedef (I chose image to match the structure tag, but you can choose any other name you like). A typedef with no name is 'valid' but not useful — you could omit typedef and get the same result.

You might use:

image *ip = malloc(sizeof(image) + width * height * sizeof(struct pixel));

if (ip != 0)
{
    ip->width = width;
    ip->height = height;
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
            ip->pixels[i*width + j] = default_pixel_value;
    }
    …use ip…
    free(ip);
}

I'm not sure that there's a good way to get a 2D array as the FAM.

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

Comments

3

You are using what is called a variable length array, however, there's a problem. In C every struct must have a specific byte length, so that, for example, sizeof(struct image) can be evaluated. In your case, the variable length array's size cannot be determined at compile time, so it is illegal.

On another note, you probably want pointers there, instead of arrays of declared length:

typedef struct image
{
    int width;
    int height;
    struct pixel **pixels;
};

Disclaimers: VLAs in structs are occasionally allowed, although it's a matter of opinion if this extension is more of a bug than a feature. See: Undocumented GCC Extension: VLA in struct

Edit: remyabel points out the GCC docs that talk about the rare situations in which VLAs in structs are okay: https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html#Variable-Length

1 Comment

Consider linking to the gcc docs. I believe they have now added documentation for VLAs in structs.
2

i would suggest an alternative like this:

typedef struct 
{
int width;
int height;
struct pixel *pixels;
} image;

every time, you need to alloc:

image img;
/* init */
img.pixels=malloc(sizeof(struct pixel)*img.width*img.height);

*(img.pixels+img.height*i+j) represents img.pixels[i][j].

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.