1

Why is it that both the memcpy below produces the same result:

int arr1[5] = {some_data1};
int arr2[5] = {some_data2};

memcpy(&arr1, &arr2, (5 * sizeof(int)));
memcpy(arr1, arr2, (5 * sizeof(int)));

Why doesn't the first memcpy see a double pointer for arr1 and arr2? Is it because the array name is not truly a pointer but rather an identifier for a variable of the "array" type?

Stas Bukhtiyarov
  • 97
  • 2
  • 2
  • 8
  • 1
    possible duplicate of [How come an array's address is equal to its value in C?](http://stackoverflow.com/questions/2528318/how-come-an-arrays-address-is-equal-to-its-value-in-c) – Barmar Jun 13 '14 at 12:40
  • 2
    (offtopic: passing `5` as size in memcpy is wrong) –  Jun 13 '14 at 12:42

5 Answers5

3

Why doesn't the first memcpy see a double pointer for arr1 and arr2?

Bluntly speaking, because it is not a double pointer.

Is it because the array name is not truly a pointer but rather an identifier for a variable of the "array" type?

Absolutely! This happens precisely because arrays are not pointers, despite many misleading similarities shared by the two kinds of C objects.

The behavior is the same because both expressions produce numerically identical void* pointers when passed to memcpy.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Because in C, when a is an array we have this:

 (void *) a == (void *) &a

the address of &a (pointer to the array) and of &a[0] (pointer to the first element of the array) is the same, only the type is different.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

Duplicate of this question - "The name of an array usually evaluates to the address of the first element of the array, so array and &array have the same value."

Community
  • 1
  • 1
jarmod
  • 71,565
  • 16
  • 115
  • 122
0

In some contexts, when you use an array name it is converted to the address of the array. Passing an array as a function argument is one of those contexts. But using the array with the & operator is not -- it returns the address of the array.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

in array, your array variable(arr1 amd arr2) naturally represent base address of the array, you do not need to give &(address of operator). arr1==&arr1

user3614789
  • 190
  • 1
  • 11