FM> How does one get a program to get the ADDRESS of a char? I
FM> can do it readily with an int.
It works the same way for a char as for an int.
int i;
int *p = &i;
char c;
char* q = &c;
FM> If I assign char x - 'A' thought and
FM> do a cout << &x I get an A followed by one of the early chars.
That's because operator<< for a char* (or const char*) is built to
output zero terminated arrays of characters. It'll keep on
displaying characters till it hits that zero byte. And where is
that zero byte?? (Due to alignment considerations, chances are there
are one or three bytes of padding (with undefined contents) between
the declaration of the char and the declaration of the next variable.)
It looks like what you want is to just display a character, not what
the character is pointing to.
char c = 'B';
cout << c; //displays the 'A'
cout << char(c - 1); //displays 'B' (assuming ASCII chars)
Note the need tp cast the result of the arithmetic to a char. Otherwise,
the char is silently promoted to an int for the subtraction and the
<< operator thinks you want to output an int instead of a char.
cout << &c; //displays 'A' and then who know what;
char* p = &c;
cout << *p; //displays the 'A'
cout << (void*)p; //displays the value of the pointer!
---
þ Blue Wave/QWK v2.12 þ
---------------
* Origin: St. Louis Users Group (1:100/4)
|