& operator question

Started by
8 comments, last by FrikkaSoft 22 years, 5 months ago
#include using namespace std; // in main char symbol = ''a''; cout << &symbol << endl; //I don''t get the memory address //of the location at witch symbol //is placed in memory. //Instead i get ''a'' on the screen. I thought & operator always gives me the memory location of a variable. If I make array of characters and place the & operator in front that variable (like above) I get the memory location. Any ideas. Thanks in advance.
Friðrik Ásmundsson
Advertisement
Remember a string of characters is accessed trough a pointer, so it is logical that if you feed a pointer to char to cout it will print the string, or more specific the memory from the pointed location until it finds a null character "\0", if you want to get the memory location you might have to do some casting before cout like so:

// in mainchar symbol = 'a';int nMemorylocation = (int) &symbolcout << nMemorylocation << endl;  

not sure if works, but something like that is what you have to do.

Edit:
oh yeah, there are some casting statements for cout, but I dont remember those, I think HEX,OCT,DEC or something, in any case I stick to good old trusty printf


Edited by - kwizatz on November 14, 2001 9:35:46 AM
void main(){
char* symbol = "b";
cout << &symbol << endl;
}
quote:Original post by superpeter
void main(){
char* symbol = "b";
cout << &symbol << endl;
}


That outputs the address of the pointer to the "b", not the address of the "b".
Thanks for the replies.

Now I have this...

char symbol= ''a'';
char s[] = "a";
int nMemorylocation = (int) &symbol
cout << nMemorylocation << endl; //6553091, I non valid location
cout << &s << endl; //0063FE01, a valid location

I know when doing char s[]="a" I am making a string of
characters. And I can retrieve the memory location of the
first character in this string with &S.
But when I want do retrieve the memory location of the first and only character in the character variable "symbol", I do not get
a valid memory address (I might be wrong though).

Again thanks for the replies. All comments are helpful.
Friðrik Ásmundsson
#include <iostream>using namespace std;int main(int argc, char *argv[]){	char *a = "a";	int &address = (int &)(*a);	cout << "Character: " << a << endl		<< "Address: 0x" << &address << endl;	return 0;} 
Or instead of storing an extra integer, just cast the pointer to a void pointer, then no special processing will be done:

main
{
char symbol = ''b'';
cout (void *)&symbol << endl;
}
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan

My question has been answered.

Thanks.
Friðrik Ásmundsson
FrikkaSoft: you might want to learn you''re hexadecimal to decimal conversion.

6553091 = 0x0063fe03.

6553091 = decimal.
0x0063fe03 = hex.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
I know, I am extremely ashamed in the moment
I think my effort of thinking was lacking during that post by Kwizatz. Well...

Batnandi mönnum er best að lifa.
Friðrik Ásmundsson

This topic is closed to new replies.

Advertisement