Write the prototype for a function that takes an array of exactly 16 integers

后端 未结 5 1955
悲&欢浪女
悲&欢浪女 2020-12-30 00:44

One of the interview questions asked me to \"write the prototype for a C function that takes an array of exactly 16 integers\" and I was wondering what it could be? Maybe a

5条回答
  •  盖世英雄少女心
    2020-12-30 00:56

    There are several ways to declare array-parameters of fixed size:

    void foo(int values[16]);
    

    accepts any pointer-to-int, but the array-size serves as documentation

    void foo(int (*values)[16]);
    

    accepts a pointer to an array with exactly 16 elements

    void foo(int values[static 16]);
    

    accepts a pointer to the first element of an array with at least 16 elements

    struct bar { int values[16]; };
    void foo(struct bar bar);
    

    accepts a structure boxing an array with exactly 16 elements, passing them by value.

提交回复
热议问题