Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange variable-sized array declaration

Tags:

arrays

c

Reading this Skip List implementation I came across this code fragment:

typedef struct nodeStructure{
    keyType key;
    valueType value;
    node forward[1]; /* variable sized array of forward pointers */
    };

To me it seems that forward[1] denotes a one element array. And the comment calls it a variable sized array.

Do I misunderstand something or this is just a mistake in the source I'm reading?

like image 445
ovgolovin Avatar asked Dec 15 '22 17:12

ovgolovin


1 Answers

It is called the struct hack. It is the old form of the flexible array member introduced in C99.

This has been used in the past to mimic a variable array in the last member of a structure but it is not a strictly conformning construct in C.

like image 187
ouah Avatar answered Jan 02 '23 01:01

ouah