Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

c - Array of variable length in struct

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

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

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

I am getting an error saying that width and heigth are not defined in the definition of image. I don't get why I'm getting that error and how I can solve it

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 spelling fixup for height; note also 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...