using cout i get symbols instead address [Solved]
-
Hello I'm learning pointers and i have few questions. In this example i create 1 int variable and 2 char variables
#include <iostream> using namespace std; int main () { int b=10; int *d; char a='B',c='D'; char *p; cout <<"address of int variable b: "<<&b<<endl; cout <<"address of char variable a: "<<&a<<endl; cout <<"address of char variable c: "<<&c<<endl; p=&c; cout <<"address of int variable painted by pointer p: "<<p<<endl; d=&b; cout <<"address of char variable painted by pointer d: "<<d<<endl; return 0; }
I get this result:address of int variable b: 0x22ff44
address of char variable a: B♀═@
address of char variable c: DB♀═@
address of int variable painted by pointer p: DB♀═@
address of char variable painted by pointer d: 0x22ff44int type variable address is 0x22ff44. Why do get address symbols B♀═@ with char variables? and if i'm wright
cout <<&a
andcout << &c
stops displaying symbols when reaches end line symbol '\n' ? This is because char variable is 1 byte long and int 4 bytes ? Right now I'm bit unfamiliar with memory management in c++.modified on Sunday, October 18, 2009 1:37 PM
-
Hello I'm learning pointers and i have few questions. In this example i create 1 int variable and 2 char variables
#include <iostream> using namespace std; int main () { int b=10; int *d; char a='B',c='D'; char *p; cout <<"address of int variable b: "<<&b<<endl; cout <<"address of char variable a: "<<&a<<endl; cout <<"address of char variable c: "<<&c<<endl; p=&c; cout <<"address of int variable painted by pointer p: "<<p<<endl; d=&b; cout <<"address of char variable painted by pointer d: "<<d<<endl; return 0; }
I get this result:address of int variable b: 0x22ff44
address of char variable a: B♀═@
address of char variable c: DB♀═@
address of int variable painted by pointer p: DB♀═@
address of char variable painted by pointer d: 0x22ff44int type variable address is 0x22ff44. Why do get address symbols B♀═@ with char variables? and if i'm wright
cout <<&a
andcout << &c
stops displaying symbols when reaches end line symbol '\n' ? This is because char variable is 1 byte long and int 4 bytes ? Right now I'm bit unfamiliar with memory management in c++.modified on Sunday, October 18, 2009 1:37 PM
Tadysas wrote:
Why do get address symbols B♀═@ with char variables?
Because cout handles int* differently than char*. A char* is considered to be a string, so cout will output the content of the string. Since you do not have a terminating 0 in your 'string', cout will output some garbage after the first character.
Tadysas wrote:
and if i'm wright cout <<&a and cout << &c stops displaying symbols when reaches end line symbol '\n' ?
It stops when it encounter a '\0' character which terminates the string.
Tadysas wrote:
This is because char variable is 1 byte long and int 4 bytes ?
No, this is because cout handles pointers to character differently than pointers to integer.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
Hello I'm learning pointers and i have few questions. In this example i create 1 int variable and 2 char variables
#include <iostream> using namespace std; int main () { int b=10; int *d; char a='B',c='D'; char *p; cout <<"address of int variable b: "<<&b<<endl; cout <<"address of char variable a: "<<&a<<endl; cout <<"address of char variable c: "<<&c<<endl; p=&c; cout <<"address of int variable painted by pointer p: "<<p<<endl; d=&b; cout <<"address of char variable painted by pointer d: "<<d<<endl; return 0; }
I get this result:address of int variable b: 0x22ff44
address of char variable a: B♀═@
address of char variable c: DB♀═@
address of int variable painted by pointer p: DB♀═@
address of char variable painted by pointer d: 0x22ff44int type variable address is 0x22ff44. Why do get address symbols B♀═@ with char variables? and if i'm wright
cout <<&a
andcout << &c
stops displaying symbols when reaches end line symbol '\n' ? This is because char variable is 1 byte long and int 4 bytes ? Right now I'm bit unfamiliar with memory management in c++.modified on Sunday, October 18, 2009 1:37 PM
if you pass a "pointer to char" to cout, you'll get what starts there in memory until a 0x0 is encounterd (it it is the way null-terminated strings are printed out. Use this instead: cout <<"address of char variable a: "<<
(void *)
(&a)<<endl;SkyWalker
-
if you pass a "pointer to char" to cout, you'll get what starts there in memory until a 0x0 is encounterd (it it is the way null-terminated strings are printed out. Use this instead: cout <<"address of char variable a: "<<
(void *)
(&a)<<endl;SkyWalker