Array of variable length in struct

后端 未结 3 1359
失恋的感觉
失恋的感觉 2020-12-09 00:31

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;
    un         


        
相关标签:
3条回答
  • 2020-12-09 00:53

    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.

    0 讨论(0)
  • 2020-12-09 00:56

    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 heigth;
        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

    0 讨论(0)
  • 2020-12-09 01:09

    i would suggest an alternative like this:

    typedef struct 
    {
    int width;
    int heigth;
    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].

    0 讨论(0)
提交回复
热议问题