Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter declaration [] and * (array)

Tags:

arrays

c

Between formal parameters in a function definition, like:

void change (int *s)
{   
    s[0] = 42;
}

And another definition:

void change (int s[])
{   
    s[0] = 42;
}

They are the same I assume, as *(a+0) is the same as a[0].

Is there a reason to prefer one over the another? Please note, the preference pertains to coding style.

like image 963
user225312 Avatar asked Jun 12 '26 14:06

user225312


1 Answers

Yes, they are exactly the same. All function parameters declared as arrays are adjusted to the corresponding pointer type.

Personally, I prefer the former which actually makes it look like a pointer declaration which is what it is in both cases.

like image 126
CB Bailey Avatar answered Jun 15 '26 03:06

CB Bailey