why cant we pass &array to function where &array[0] is possible

后端 未结 6 1568
攒了一身酷
攒了一身酷 2021-01-25 18:34
void fun(int* array){}  


int main(){

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

error: cannot convert â

6条回答
  •  一向
    一向 (楼主)
    2021-01-25 19:07

    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.

提交回复
热议问题