1
void fun(int* array){}  


int main(){

int array[]={1,2,3};
fun(&array);----->(1)//error in this line
return 0;
}

error: cannot convert âint (*)[3]â to âint*â for argument â1â to âvoid fun(int*â).

if i am passing fun(&array[0]),its working fine.As per my understanding &array and &array[0] both yield the same address.Kindly clarify.

7
  • 1
    @Adriano: Not in C++. It should be int (*)[3]. And it is (see the error message) Commented Feb 7, 2013 at 16:59
  • @Adriano array & &array both yield the same address.I have printed the values . Commented Feb 7, 2013 at 17:00
  • 5
    Adriano, your incorrect comment is terrible. Please don't write such things unless you actually know what you are talking about. Commented Feb 7, 2013 at 17:00
  • @Adriano: Which array, the first or the second? Commented Feb 7, 2013 at 17:00
  • @KerrekSB: &array only occurs in the code once, inside main. Commented Feb 7, 2013 at 17:03

6 Answers 6

5

While the address of any aggregate (this means arrays and standard-layout classes) is guaranteed to be the same as the address of the first element, the types are different, and there is no implicit conversion.


As an example why the different type is important and useful:

for( int *p = array; p < (&array)[1]; ++p )

iterates over all elements of array, while

for( int *p = array; p < (&array[0])+1; ++p )

only executes once.

Sign up to request clarification or add additional context in comments.

Comments

2

In C++11 if you run this:

#include<iostream>
template<class T>
void foo(T t) {
  std::cout << __PRETTY_FUNCTION__ << std::endl;
}

int main() {
   int arr[] = {1,2,3};
   foo(arr);
   foo<decltype(arr)>(arr);
   foo(&arr);
   foo(&arr[0]);
}

It will produce the result:

void foo(T) [with T = int*]
void foo(T) [with T = int [3]]
void foo(T) [with T = int (*)[3]]
void foo(T) [with T = int*]

Interestingly, the array collapses into a pointer by default (not sure if this is gcc behaviour or expected). However, the second line shows that the type of arr is int[3], and so &arr is a pointer to int[3] which is different than int*.

Comments

1

They don't work because they imply different element sizes. Indexing an int* increases it's address by sizeof(int), as it indexes into an array of int. Indexing an int(*)[3] increases it's address by sizeof(int) * 3, as it indexes into an array of arrays of 3 ints. Thus, even though the starting address is the same, they are very different types and imply very different pointer arithmetic. The compiler is quite correct to tell you that they are not compatible.

3 Comments

This is the best answer really.
This answer is slightly wrong. That is not the reason it doesn't work. It is an interesting fact, and might clarify the real reason (that they are different types), however, it is not the underlying reason. Else passing the address of an array of 1 int(or any other type that is the same size as an int) would work.
That is the specific disambiguator in this case- there are others in other cases. It is the specific reason that the types are not compatible in this specific case.
0

Arrays naturally decays to pointers when passed around. You don't have to use the address-of operator.

Comments

0

You have mismatching types.

The function expects *int (pointer to an int).

array and &array[0] is of that type, *int.

But &array is of type int(*)[3] (pointer to an array of 3 ints).

1 Comment

Thank you Alex for the info
0

try this:

void fun(int* array){} // int *array is integer pointer so it can point to int not the int pointer. 

int main(){

int array[]={1,2,3};
fun(array); //remove &. now *array of pm point to array[0] i.e int.
return 0;
}

1 Comment

It isn't compatible with int** either. (What should happen if the function tries *array = 0;, which is perfectly legal for an int**?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.