cout and the expression a the adress !
How is it possible to write via a common cout the adress of a char variable ?
The fact is that I tried to dump a adress of of char variable.
I compared the printf and cout for this purpose.
My surprise is that on some computers the result on the screen was correct => I had the adress of the char variable. But on some other machines, the result was totaly else, in fact the value and 5 more "characters". It seems that cout read more that 1 char at the specific location.
The expression of cout<<pChar<<endl; where pchar = &aChar; and pchar is a char pointer and aChar is a classical char variable initialiszed with the 'a' letter.
My question is : what's the fondamental mechanism explaining the comportment of a cout. The purpose was to express the value of the adress not the value pointed. It seems that in this case cout tries to express a usual "string" or char [] or char *
How can I express the value of a adress of a char alone (not belonging to a array of char's).
CODE as EXEMPLE => this code produces on some machine one result and on other (same OS : WIN2000, same processor, same compilator 1/ Dev C++ and/or 2/ Microsoft VC6++ )
#include <iomanip>
#include <iostream>
#include <stdio.h>
using namespace std;
//
int main(char * args[],int nargs){
//
char tableau[255]={'q','w','e','r','z','b','c','\0'};
int i_x=1;
char c_x='a';
char *pchar=NULL;
void *pvoid=NULL;
//
cout<<"\n_____________ + values via variable names ____________________\n";
printf("\nwith printf the char in c_x\t%c ",c_x);
cout<<"\nwith cout the same char in c_x \t"<<c_x<<endl;
i_x=c_x;
cout<<"\nc_x='a' et puis \ni_x = c_x "<<i_x<<" transtypage "<<static_cast<char>(i_x)<<endl;
//
pchar=&c_x;
pvoid=pchar;
cout<<"\n_____________ + values via variable pointers names deferenced ____________________\n";
printf("\nValues expressed via printf and the deferenced pointer pchar and pvoid %c \t%c",*pchar,*(reinterpret_cast<char* >(pvoid)) );
cout<<"\n_____________ - modification of the flux ____________________\n";
printf("\nprintf express the adress of c_x %p \nor the value of pchar %p \nor via pvoid %p ",&c_x,pchar,pvoid);
cout<<"\nThe same via cout &c_x "<<&c_x<<"\npchar "<<pchar<<"\npvoid "<<pvoid<<endl;
//
printf("\printf express the adress of the table via the name self, %p \nthe & operator on the first element of tableau %p \n",tableau,&tableau[0]);
cout<<"\ncout express the adress via the name self tableau =>"<<tableau<<"\nadress via operator adress of tableau[0] =>"<<&tableau[0]
<<"\nShould be correct not the value "<<endl;
cout<<"\n_____________ + modification of the flux ____________________\n";
cout<<"\nAdress via the name self tableau "
<<setiosflags(ios::adjustfield | ios::internal | ios::hex)
<<tableau
<<" adress of tableau[0] "<<&tableau[0]
<<"\nShould be correct not the value "<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
|