Archived

This topic is now archived and is closed to further replies.

& operator question

This topic is 5873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#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.

Share this post


Link to post
Share on other sites
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 main
char symbol = 'a';
int nMemorylocation = (int) &symbol;
cout << 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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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".

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites