I am newbie in C++ and just started my reviving my C++ (did many years back) again. I've just written a code to understand pointers to pointers, which looks something like below:
#include <iostream>
using namespace std;
int main (int argc, char * const argv[]) {
// insert code here...
char a='z';
char *b;
char **c;
//Now check
cout<<"a is :\t"<<a<<endl;
//b points to a
b=&a;
cout<<"b points a is:\t"<<b<<endl;
//c points pointer b
c=&b;
//Eventually now c has value 'z' which is value of a
cout<<"Ponter c to pointer b is now a is:\t"<<c<<endl;
return 0;
}
Output:
=======
a is : z
b points a is: z
Ponter c to pointer b is now a is: 0xbffff6dc
Everything seems to be fine except the second line. I was expecting some address value (something like 0xbf....) for b which actually points to char a but it outputs z. If I change my pointers type to int everything seems to be look fine and I get an address value for b also. Is it something related with a 'char' pointer type or else. Infact c should also contain value 'z' now.
Any input in this regard will be highly appreciated.
Yes, when printing a char pointer it is interpreted as a C string.
For a C string, a char* pointer indicates where the string starts and the string ends with a null character ('\0'). So if right after the z there's a 0 in memory, it will just print the z, otherwise it'll print some garbage or cause a segmentation fault.
You can cast it to some other pointer type to actually print the address (like (void*)b).