Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22175

Getting the size of the type of a zero-length array element in a structure

$
0
0

Maybe the title is confusing, but the idea is simple. Given the following structure:

struct z {
    uint64_t _a;
    uint64_t _b;
    struct {
        char _c[64];
        uint64_t _d;
    } data[0];
} __attribute__((packed));

How might one get the size of the type of the anonymous inner struct data? I do not want to name the struct and clutter the namespace, but I do need its size when computing the total length of what a serialized buffer would look like with a non-zero data trailer.

If you try this you'll find something similar to:

struct z *p;
sizeof(struct z) == 16
sizeof(*p) == 16
sizeof(p->data) == 0

These results are expected. However, what I was hoping to see was the following:

sizeof(typeof(p->data)) == 72

But, unfortunately, I still get

sizeof(typeof(p->data)) == 0

I thought it might be because the struct was unnamed, but after providing it with a name you'll find the earlier results are still true.

Is there a way to get the size of an anonymous inner zero-length structure?


Viewing all articles
Browse latest Browse all 22175


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>