printing position memory

hellow, friends...

How do I print on the screen the position of an element of a struct.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
int main()
{
  struct dados
 {
   int numI;
   float numF;
   char test;
 }teste;

  teste.numI=1;
  teste.numF=21.25;
  teste.test='a';
  cout<<"\n &test: "<<&teste;                 
  cout<<"\n &test.numI "<<&teste.numI;
  cout<<"\n &test.numF "<<&teste.numF;
  cout<<"\n &test.test "<<&teste.test;//don't showing address memory 
  cin.get(),cin.get();   
}

why "&teste" don't printing address of type char ???
That happens because operator<<() is overloaded for the very particular case of char*, which is the data type of &teste.test. This special overload will assume there are many characters in line that need displaying and won't stop until a null char (value zero) is encountered. Since you don't really have a chain of characters, the use of this particular overload is incorrect, but the compiler has no way of knowing this.

Therefore what you need to do is tell the compiler you don't that special overload by typecasting to void*:

cout << "&test.test: " << reinterpret_cast<void*>(&teste.test) << endl;
webJose, amazing!!
I tested with chain of characters and works like you said...and also used typecasting to void...and its true!!!
i will get more about overload <<...
thank you !!!!
Last edited on
Topic archived. No new replies allowed.