Character variables and addresses

This is a discussion on Character variables and addresses within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; class Multitudinous { private: int int1; float float1; public: float float2; double double1; void ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    14

    Character variables and addresses

    Code:
    #include <iostream>
    using namespace std;
    
    class Multitudinous
    {
    private:
       int int1;
       float float1;        
    public:
       float float2;
       double double1;
       void showMap();
       friend int main();       
    protected:
       char char1;       
    private:
       int int2;
       double double2;
       char char2;        
    public:
       long long1;
       double double3;       
    protected:
       int int3, int4;
       char char3, char4;          
    private:
       long long2;
       double double4;        
    public:
       float float3, float4;       
    protected:
       char char5, char6, char7;                                                                                 
    };
    
    void Multitudinous::showMap()
    {
       cout << "int1: " << &int1 << endl << "float1: " << &float1 << endl;
       cout << "float2: " << &float2 << endl << "double1: " << &double1 << endl;
       cout << "char1: " << &(char1) << endl << "int2: " << &int2 << endl;
       cout << "double2: " << &double2 << endl << "char2: " << &(char2)<< endl;
       cout << "long1: " << &long1 << endl << "double3: " << &double3 << endl;
       cout << "int3: " << &int3 << endl << "int4: " << &int4 << endl;
       cout << "char3: " << &(char3) << endl << "char4: " << &(char4) << endl;
       cout << "long2: " << &long2 << endl << "double4: " << &double4 << endl;
       cout << "float3: " << &float3 << endl << "float4: " << &float4 << endl;
       cout << "char5: " << &(char5) << endl << "char6: " << &(char6) << endl;
       cout << "char7: " << &(char7) << endl;     
    }           
    
    int main()
    {
    Multitudinous c1;
    c1.showMap();
    char* point = &(c1.char1);
    cout << "char1: " << point << endl;
    point = &(c1.char2);
    cout << "char2: " << point << endl;
    point = &(c1.char3);
    cout << "char3: " << point << endl;
    point = &(c1.char4);
    cout << "char4: " << point << endl;
    point = &(c1.char5);
    cout << "char5: " << point << endl;
    point = &(c1.char6);
    cout << "char6: " << point << endl;
    point = &(c1.char7);
    cout << "char7: " << point << endl;   
    return 0;    
    }
    The program above is meant to simply print out the addresses of the data members of the object, in order to gain some insight into memory allocation within classes.
    The problem is that the member function showMap() seems to be unable to print out the addresses of the character variables of the object, and I have no idea why this is the case. Instead of addresses, the output takes the form of a string of seemingly random characters.
    Can anyone explain why this is happening and how to fix it?

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The addresses of char variables are interpreted as strings by the stream inserters. cast to void pointers
    Code:
       cout << "char3: " << (void*)&(char3) << endl;
    Kurt

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    14
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 03:12 AM
  2. Order of Memory Locations
    By vlrk in forum Linux Programming
    Replies: 11
    Last Post: 06-30-2008, 08:11 AM
  3. whats char*?
    By Abda92 in forum C Programming
    Replies: 23
    Last Post: 09-13-2006, 01:53 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 02:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21