I am learning c++
and encountered this:
#include<iostream>
using namespace std;
int main(){
const char *a[] = {"ge","hy"};
cout<<a<<" "<<&a[1]<<endl;
cout<<a[0]<<" "<<a[1];
return 0;
}
The output is :
0x7fff54e71830 0x7fff54e71838
ge hy
I tried to understand the code.
Here is my understanding:
a
is an array
of character pointers which means that each element of the array is a char
pointer.
Now, since every element is a pointer then it should store the address of "ge" and "hy" respectively.
-----------------------------------
a = | 0x7fff54e71830 | 0x7fff54e71838 |
-----------------------------------
Now when I write a[0]
and a[1]
then why does it print the ge hy
and not the memory address of them because the array a
stores their address and not their actual value.
I am sure that I'm going wrong somewhere because the output is not as expected. Kindly, correct me here.