Skip to main content
GameDev.net gamedev.net
🔒 Locked

& operator question

Started by FrikkaSoft Nov 14, 2001 at 7:59 AM 8 replies 550+ views
Original Post
FrikkaSoft
FrikkaSoft
#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
Kwizatz
Kwizatz
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
superpeter
superpeter
void main(){
char* symbol = "b";
cout << &symbol << endl;
}
FrikkaSoft
FrikkaSoft
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
Oluseyi
Oluseyi

#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;
}
JY
JY
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
FrikkaSoft
FrikkaSoft

My question has been answered.

Thanks.
Friðrik Ásmundsson
jenova
jenova
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.
FrikkaSoft
FrikkaSoft
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.