What is the difference between declaring function as void myfunc(int *a) and void myfunc(int a[])

Apparently arrays are passed to functions by address and not by value. When then is the difference between declaring the function parameter as

void myfunc(int *a)

or as

void myfunc(int a[])

?
To the compiler, nothing. To a person reading the code the second hints that the parameter should be a pointer to an array not a pointer to single int, but it is just a hint.
I can see that it is also possible to declare a function as:

void myfunc(int a[10])

However, testing shows that even this is just a pointer. I cannot use sizeof or _count on it. Besides this, the stranger bit is that sending an array larger or smaller than a[10] actually works and does not generated any error.

What is the purpose of declaring a function parameter like int a[10] when it is same as int *a or int a[]?

NB: I meant using sizeof to find size of array
Last edited on
What is the purpose of declaring a function parameter like int a[10] when it is same as int *a or int a[]?

Again the [10] is just a hint to the person using the function that the function is being passed an array of int with a size of 10. The compiler doesn't care what you put into the brackets of a single dimensional array.

However, testing shows that even this is just a pointer.

Yes it is a pointer, remember when you pass an array to a function you are actually passing a pointer to the first element, you never actually pass an array to or from a function. You loose any sizeof information because all you have is the pointer not the array. You can only use sizeof() to determine the size of a statically allocated array, otherwise it is up to you, the programmer, to keep track of the size of the array.

you never actually pass an array to or from a function

this misses "...by value": C++, you can actually pass arrays (size and all) by reference: void myfunc(int (&a)[10]). It's just that functions taking arrays by value do not exist and C chose to compile some other functions instead of making this a hard error.
hmm
"when you pass an array to a function you are actually passing a pointer to the first element, you never actually pass an array to or from a function. You loose any sizeof information because all you have is the pointer not the array."
is the key point here. Thanks.

Is there any reason why I should choose to not pass array by reference? I mean as long as I use the word const, that should be sufficient to prevent the data from being changed in the function. I don't think that there is anything that I cannot do if I pass the array as reference to the function that I could if I pass the array using the usual pass by address method.
Array by reference is horribly rigid:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void foo( int (&bar)[10] ){}

int main ()
{
    int aa[5];
    int bb[10];
    int cc[15];
    int * dd = 0;
    foo( bb ); // ok
    // the following have error: invalid initialization of reference
    foo( aa ); // of type 'int (&)[10]' from expression of type 'int [5]'
    foo( cc ); // of type 'int (&)[10]' from expression of type 'int [15]'
    foo( dd ); // of type 'int (&)[10]' from expression of type 'int*'
}


Try to design generic code. It is more reusable.
The standard library passes arrays by reference, in std::begin, std::cbegin, std::end, std::cend, std::rbegin, std::rend, std::crend, and the C++17 functions std::size, std::data, and std::empty. They are all, of course, templated on the size.
Topic archived. No new replies allowed.