Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 23rd, 2004, 05:40 AM
Registered User
 
Join Date: Sep 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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;
}


Reply With Quote
  #2 (permalink)  
Old September 25th, 2004, 02:35 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this:

Code:
#include <iostream>
using namespace std;

int main()
{
  char hello[] = "Hello";

  char *chpoint = hello;

  cout << "First with cout:" << endl;
  cout << "Here's cout << chpoint        : " << chpoint << endl;
  cout << "Here's cout << (void *)chpoint: " << (void *)chpoint << endl;

  printf("\n\nNow, with printf():\n");
  printf("Here's chpoint with printf(%%s): %s\n", chpoint);
  printf("Here's chpoint with printf(%%p): %p\n", chpoint);

  return 0;
}

if you use cout with a variable of type char *, it is designed to print a c-style string.

If you want to print the value of a pointer, cast it to void *.

This works for me, but I don't know what type of machines you have and whether it will work for them.

Regards,

Dave
Reply With Quote
  #3 (permalink)  
Old September 25th, 2004, 03:00 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

To clarify: in your program, &c_x does not point to a c-style string. cout starts with the address of c_x and prints out characters until a '\0' is found. Different compilers may allocate local storage in different ways; since it is not possible for us to know where the next '\0' is located, the result is undefined (that is, anything could happen).

If you run the code that I suggested and have any questions: just ask.

Dave
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Message> in query expression <expression>. (Error ybg1 Access 5 July 15th, 2007 05:42 AM
Getting the mac adress of the client maher29681 J2EE 0 June 14th, 2006 04:12 AM
Expression Filter dba123 Reporting Services 5 March 3rd, 2006 11:35 AM
regular expression vohra_vikas2004 ADO.NET 3 November 18th, 2004 09:59 PM
'OR' Expression saturdave ASP.NET 1.0 and 1.1 Basics 2 February 17th, 2004 03:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.