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

后端 未结 5 1957
悲&欢浪女
悲&欢浪女 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 01:02

    & is necessary in C++:

    void foo(int (&a)[16]); // & is necessary. (in C++)
    

    Note : & is necessary, otherwise you can pass array of any size!


    For C:

    void foo(int (*a)[16]) //one way
    {
    }
    
    typedef int (*IntArr16)[16]; //other way
    void bar(IntArr16 a)
    {
    }
    
    int main(void) 
    {
            int a[16];
            foo(&a); //call like this - otherwise you'll get warning!
            bar(&a); //call like this - otherwise you'll get warning!
            return 0;
    }
    

    Demo : http://www.ideone.com/fWva6

提交回复
热议问题