In C++, I wrote the following simple main:
int main() {
char test[100];
void* a = (void*) test;
void* b = (void*) &test;
std::cout << a << " " << b << std::endl;
return 0;
}
And it gives me the same result for a
and b
. Why is this? I would expect from the notation that the second be the address of the first..
In C++, arrays are converted to pointer to first element of the array. test
is pointer to first element test[0]
. &test
is the address of entire array test
. Although, the type of test
and &test
are different, their values are same and that's why you are getting the same value.
For example
int a[3] = {5, 4, 6};
Look at the diagram below:
For detailed explanation read this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With