Am learning C++ from "Sams Teach Yourself C++" and I ran into a problem wihle trying to learn about pointers that isn't addressed here or in the book. I can't seem to get the pointer to a char variable to work right?
The contents of the variables work fine, but the &CVar1 and &CVar2 yields not hex address as one would hope, but rather a string special characters which I can't seem to duplicate on my keyboard here (like: a]]]]]]]]]}}}}}}}}}} only weirder). Notably both the output of &CVar1 and &CVar2 begin with the letters with which they are initialized. Any ideas? Thanks.
Hmm I had this same issue a while back when doing something like this for Homework. I never found the problem and my teacher had never heard of it either
C/C++ can be a pain regarding char*.
Most C/C++ function in the standard library treats char* as a pointer to a null terminated string. cout is no different.
So:
1 2
char x:
cout << &x;
will try to print out a string (which might cause a segementation fault if an unitilaised char* is used) - NOT an address.
To print out char* as an address you cast it to void*
1 2 3
char* p = "Hello World";
cout << p << endl; //prints the text.
cout << (void*)p << endl; //prints the value of the pointer