difference between &array[0] and &array when passed to a C function

后端 未结 5 1382
礼貌的吻别
礼貌的吻别 2020-12-17 04:20

Is there a difference between &array[0] and &array when passed to a C Function. This array is a void* array which currently takes integer as data.

Added the

5条回答
  •  失恋的感觉
    2020-12-17 05:06

    This won't compile, you are using a void * and try to get the first element of it. But what size does it have? The compiler doesn't know. Using int * may compile, if you are not trying something like this:

    int main (void) {
      int *arr = malloc( 10 );
    
      arr = &arr[0]; // this is ok
      arr = &arr;    // wrong data type
    }
    

    &array returns an int **, &array[0] returns int *. These are different data types.

提交回复
热议问题