delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/16/12:18:10

Newsgroups: comp.os.msdos.djgpp
From: rgu AT world DOT std DOT com (Rick Umali)
Subject: Printing Pointer Address - cout (gxx version 2.7.2.1)
Message-ID: <ErIKAn.F8J@world.std.com>
Keywords: cout, pointers, addresses
Organization: The World Public Access UNIX, Brookline, MA
Date: Thu, 16 Apr 1998 15:56:42 GMT
Lines: 74
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hello:

I have the following simple code. Some of you may
recognize this as listing 8.3 from Jesse Liberty's
book _C++ in 21 Days_:

  1  // Listing 8.3 What is stored in a pointer.
  2  #include <iostream.h>
  3  typedef unsigned short int USHORT;
  4  int main()
  5  {
  6     unsigned short int myAge = 5, yourAge = 10;
  7     unsigned short int * pAge = &myAge;  // a pointer
  8
  9     cout << "myAge:\t" << myAge <<  "\tyourAge:\t" << yourAge << "\n";
 10     cout << "&myAge:\t" << &myAge <<  "\t&yourAge:\t" << &yourAge <<"\n";
 11     cout << "pAge:\t" << pAge << "\n";
 12     cout << "*pAge:\t" << *pAge << "\n";
 13     pAge = &yourAge;       // reassign the pointer
 14     cout << "myAge:\t" << myAge <<  "\tyourAge:\t" << yourAge << "\n";
 15     cout << "&myAge:\t" << &myAge <<  "\t&yourAge:\t" << &yourAge <<"\n";
 16     cout << "pAge:\t" << pAge << "\n";
 17     cout << "*pAge:\t" << *pAge << "\n";
 18     cout << "&pAge:\t" << &pAge << "\n";
 19
 20     return 0;
 21  }

On my Solaris machine at work, I see:

% a.out
myAge:  5       yourAge:        10
&myAge: 0xeffff83a      &yourAge:       0xeffff838
pAge:   0xeffff83a
*pAge:  5
myAge:  5       yourAge:        10
&myAge: 0xeffff83a      &yourAge:       0xeffff838
pAge:   0xeffff838
*pAge:  10
&pAge:  0xeffff834

But on my PC with djgpp (version 2.7.2.1):

C:\Rick\c++\liberty\Chap8>a.exe
myAge:  5       yourAge:        10
&myAge: 1       &yourAge:       1
pAge:   1
*pAge:  5
myAge:  5       yourAge:        10
&myAge: 1       &yourAge:       1
pAge:   1
*pAge:  10
&pAge:  1

Now I have figured out that replacing lines like this:

 18     cout << "&pAge:\t" << &pAge << "\n";

with:

 18     cout << "&pAge:\t" << (int) &pAge << "\n";

causes the printing of what looks to be an address, but 
I'm curious why my pointers don't print. Why do I have to do 
a cast to get the address?

(I didn't down load source to find this answer for myself.)

Thanks in advance,

-- 
Rick (rgu AT world DOT std DOT com) Umali     Arlington, Massachusetts
     "I got the black cat bone and I got a mojo, too."
           http://world.std.com/~rgu/tigerwoods/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019