c - Regarding struct hack using int type -


can struct hack(using int type) below

struct node{    int i;    struct node *next;    int p[0]; }  int main(){    struct node *n = // correct hack i.e. p[10]?       malloc(sizeof(struct node) + sizeof(int) * 10); } 

also using int type size 1

struct node{    int i;    struct node *next;    int p[1]; }  int main(){    struct node *n = // correct hack i.e. p[10]?       malloc(sizeof(struct node) + sizeof(int) * 10); } 

the former struct hack used in c89. validity of construct has been questionable.

the latter gnu struct hack, makes use of gnu extension , not valid c.

the correct way have structures size vary @ run time use c99 flexible array member feature.

struct node{     int i;     struct node *next;     int p[]; }  int main(void) {      struct node *n = malloc(sizeof (struct node) + sizeof (int) * 10); } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -