chaox
chaox

Reputation: 51

Empty block {} transforms to struct

I am reading some C language codes(seems written according to C11) about compiler principle got from Github,links below.

https://github.com/rui314/8cc

I get some codes begin from main.c like:

Firstly,a struct:

typedef struct { void **body; int len; int nalloc; } Vector;

and then a macro:

#define EMPTY_VECTOR ((Vector){})

and last:

static Vector *cond_incl_stack = &EMPTY_VECTOR;

My question is that if it is possible to transform a empty block to a struct and assigned to a pointer and I write some codes to test:

Vector v=(Vector){}; Vector a[3]={};

and there is no compliant in Qt5

Upvotes: 1

Views: 80

Answers (1)

Lundin
Lundin

Reputation: 213892

An empty initializer list is not allowed in any version of C.

I believe this is a GNU non-standard extension. Don't use it. If you want the standard equivalent, initialize at least one of the elements:

Vector v = {0};

Same goes for compound literals:

Vector v = (Vector){0};

Edit: quote from the standard:

(6.7.9) initializer-list:
        designationopt initializer
        initializer-list , designationopt initializer  

Upvotes: 5

Related Questions