Home > Software engineering >  Passing the address of an array into a function in C
Passing the address of an array into a function in C

Time:05-28

I'm new to c and I am confused by the idea of passing a pointer array into a function.

Here's the function

void func(int *a){ a[0]=999;}

which is used by the main

int main()
{
    int a[5]={1,2,3,4,5};
    func(a);
    std::cout << a[0]<< std::endl;
}

I understand this works perfectly as the name of an array is just the address of its first element. Based on my understanding, &a refers to the address of the entire array while a refers to the address of the first element of the array, which should be identical to &a. However, if I use

int main(){
    int a[5]={1,2,3,4,5};
    func(&a);
    cout<< a[0]<< endl;
}

It returns the compiler error: no known conversion from 'int (*)[10]' to 'int *' for 1st argument; remove &

Could anyone please explain what's going on here?

CodePudding user response:

Case 1

I understand this works perfectly as the name of an array is just the address of its first element.

The above statement is not technically correct. The first example worked because in many contexts(including when passing an array as an argument by value to a function) the array decays to a pointer to its first element due to type decay. This means, in example 1, when you passed a as an argument, there was an implicit array-to-pointer conversion which converted your array a of type int[5] to a pointer to its first element which is the type int*. Thus in example 1, the type of the argument and the type of the parameter matches and example 1 succeeded.

Note also that even though the decayed pointer and the address of the array both have the same value their types are different. The decayed pointer is of type int* while the &a is of type int (*)[5].

Case 2

Could anyone please explain what's going on here?

This is because a is of type int [5] which means &a is of type int (*)[5] which doesn't match with the type of the parameter int*.

That is, when you modified your code, you were passing &a which is of type int (*)[5] but the function parameter is of type int* and hence there is a mismatch in the type of the parameter and the argument you're passing and as there is no implicit conversion from a int (*)[5] to an int* we get the mentioned error saying:

no known conversion from 'int (*)[5]' to 'int *'

CodePudding user response:

I understand this works perfectly as the name of an array is just the address of its first element.

This is a subtly wrong understanding. The array int[10] can implicitly convert to a pointer to (address of) the first element int*. This implicit conversion happens when you call the function that accepts int* parameter and passing int[10] argument.

&a refers to the address of the entire array while a refers to the address of the first element of the array, which should be identical to &a

The value of the pointers is identical. But the type of the pointers is different. One is a pointer to an array int (*)[10] and the other is a pointer to an element of such array int*. Neither type implicitly converts to the latter, and hence you cannot pass &a as an argument to the function that expects int*. The type system of the language prevents you from making a mistake. This is explained by the compiler diagnostic message.

CodePudding user response:

See a as a pointer. It points to the first element of the array, a[0] (or *a).

Then &a is the address of the pointer, which is unrelated to the above.

Note that (&a)[0] or *(&a) return the pointer, while (&a)[0][0] or (*(&a))[0] or *((&a)[0]) or **(&a) return the first element of the array.

  • Related