How to determine the length of an array at compile time?

前端 未结 4 1234
一生所求
一生所求 2020-12-18 04:30

Are there macros or builtins that can return the length of arrays at compile time in GCC?

For example:

int array[10];

For which:

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-18 05:28

    I wouldn't rely on sizeof since aligment stuff could mess up the thing.

    #define COUNT 10
    int array[COUNT];
    

    And then you could use COUNT as you want.

提交回复
热议问题