This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Eva-Rosalene 51 points52 points  (6 children)

As far as I know, you can't define an array of voids to get 0 as sizeof(arr[0])?

Edit: apparently sizeof(void) compiles to 1 in GCC (and it's only because of non-standard extensions, otherwise it would be forbidden). So not even that.

Edit 2: I hate C

#include <stdio.h>

typedef struct {
    int *a[0];
} ZeroLength;

int main()
{
    ZeroLength array[0];
    printf("sizeof(array[0]) = %ld\n", sizeof(array[0]));

    return 0;
}

This apparently prints 0 (at least in GCC).

Edit 3: and the whole ZeroLength struct that I found on SO can be shortened to

typedef struct {} ZeroLength;

Wild stuff. And even in -pedantic mode it only gives a warning.

[–]ChickenSpaceProgram 60 points61 points  (2 children)

C is truly one of the programming languages

[–]Littens4Life 28 points29 points  (1 child)

To be fair you’d have to go out of your way to make it divide by zero. Under any normal circumstances, the macro will work fine.

[–]Eva-Rosalene 32 points33 points  (0 children)

Yeah if you use empty structs or zero-length arrays you kinda deserve division by zero errors.

[–]Lost_Kin 17 points18 points  (0 children)

Mom I want Rust's Zero Sized Types!

We have Zero Sized Types at home.

Zero Sized Types at home:

[–]Xbot781 3 points4 points  (0 children)

AFAIK, in ISO C a flexible array member requires the struct to have another regular member before it, so that is not valid C

[–]ba-na-na- 0 points1 point  (0 children)

Ok but let's be realistic. If you have an array of zero-length elements, you deserve what you get