Reputation: 97
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?
Upvotes: 1
Views: 134
Reputation: 726967
Why doesn't the first memcpy see a double pointer for
arr1
andarr2
?
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
.
Upvotes: 3
Reputation: 200
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
Upvotes: 0
Reputation: 782166
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.
Upvotes: 0
Reputation: 78842
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."
Upvotes: 0
Reputation: 145899
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.
Upvotes: 1