How can I find the number of elements in an array?

前端 未结 14 1159
广开言路
广开言路 2020-12-02 15:01

I have an int array and I need to find the number of elements in it. I know it has something to do with sizeof but I\'m not sure how to use it exac

14条回答
  •  执笔经年
    2020-12-02 15:52

    I personally think that sizeof(a) / sizeof(*a) looks cleaner.

    I also prefer to define it as a macro:

    #define NUM(a) (sizeof(a) / sizeof(*a))
    

    Then you can use it in for-loops, thusly:

    for (i = 0; i < NUM(a); i++)
    

提交回复
热议问题