I was trying to execute the following code:
#include <iostream>
using namespace std;
int main()
{
int arr[4] = {1,2,3,4};
int *p;
p = arr;
cout << "p=" << p << endl;
char ch3[4] = {'c','d','e'};
char *ptr;
ptr = ch3;
cout << ptr << endl;
getchar();
return 0;
}
When I print the pointer p, it prints the address of the array 'arr' which is stored in it, whereas when I print the pointer ptr, it prints the array ch3 and not the address of it. I wanted to know why is this happening.
Because operator<<
is overloaded for const char*
- that overload prints the char array located at that address.
To see the address itself, you'll need to cast it to void*
:
cout<<static_cast<void*>(ptr)<<endl;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.