9

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
moray95
  • 937
  • 2
  • 9
  • 12
  • 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? – Jason Hu Oct 03 '14 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. – jee7s Oct 03 '14 at 17:32

3 Answers3

12

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.

endolith
  • 25,479
  • 34
  • 128
  • 192
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
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

endolith
  • 25,479
  • 34
  • 128
  • 192
FakeName123
  • 137
  • 5
  • Consider linking to the [gcc docs](https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html). I believe they have now added documentation for VLAs in structs. –  Oct 03 '14 at 17:37
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].

endolith
  • 25,479
  • 34
  • 128
  • 192
Jason Hu
  • 6,239
  • 1
  • 20
  • 41