Why is this valid in C
int * foo(int a,int b){
...
}
but this is invalid
int [] foo(int a,int b){
...
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The syntax is somewhat funny.
But it’s not allowed anyway. See n1256 6.7.5.3 paragraph 1, “Function declarators”.
You can return pointers to arrays:
But you might as well just return a pointer instead, since the following are equivalent:
Typically, if a function needs to return an array of
int, you either return the pointer or pass the pointer in. One of the following:The “struct-hack” also works for arrays that have a fixed size:
But this is not recommended unless the array is small.
Rationale:
Arrays are often large, and often have a size not known until runtime. Since parameters and return values are passed using the stack and registers (on all ABIs I know of), and the stack has a fixed size, it is somewhat dangerous to pass such a large object around on the stack. Some ABIs also don’t gracefully handle large return values, potentially causing extra copies of return values to be generated.
The following code can also be dangerous: