GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Apr-2008, 21:44
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 405
Peter_APIIT is on a distinguished road

Prove Union has Single Address


Hello to dear expert programmer, i have wrote a program that test the address of union member.

I cannot print out address of union member.

Below is what i have wrote :

CPP / C++ / C Code:
#include<iostream>
#include<set>
#include<windows.h>
 
using namespace std;
 
// ------------------------------------------------
 
 
union Package
{
    char i;
    short j;
    int k;
    float f;
    long double mathematicalEquation;
};
 
/*
    Sizeof Union determines by largest sotrage 
    class in an union
*/
 
// ------------------------------------------------
int main(int argc, char *argv[])
{
    cout << "Sizeof Package " << sizeof(Package);
 
    Package aPackage;
 
    cout << "\n" << "Address of Package : " << &aPackage;
    cout << "\n" << "Address of I : " << &aPackage.i;
 
 
    return 0;
}
// ------------------------------------------------
 

Thanks for your help.
__________________
Linux is the best OS in the world.
Last edited by admin II : 29-Apr-2008 at 03:48. Reason: Changed [CODE] to [CPP]
  #2  
Old 28-Apr-2008, 21:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Prove Union has Single Address


Quote:
Originally Posted by Peter_APIIT
I cannot print out address of union member.
To print the value of a pointer, cast it to (void *):

CPP / C++ / C Code:
#include<iostream>

using namespace std;

union Package
{
    char i;
    short j;
    int k;
    float f;
    long double mathematicalEquation;
};


int main()
{
    cout << "Size of Package = " << sizeof(Package) << endl << endl;
    
    Package aPackage;

    cout << "Addr of aPackage   : " << reinterpret_cast<void *>(&aPackage)
         << endl;
    cout << "Addr of aPackage.i : " << reinterpret_cast<void *>(&aPackage.i)
         << endl;
    cout << "Addr of aPackage.j : " << reinterpret_cast<void *>(&aPackage.j)
         << endl;
    cout << "Addr of aPackage.k : " << reinterpret_cast<void *>(&aPackage.k)
         << endl;
    cout << "Addr of aPackage.f : " << reinterpret_cast<void *>(&aPackage.f)
         << endl;
    cout << "Addr of aPackage.mathematicalEquation : " 
         << reinterpret_cast<void *>(&aPackage.mathematicalEquation)
         << endl;
    return 0;
}

Output (from GNU g++)
Code:
Size of Package = 12 Addr of aPackage : 0xbf9d6848 Addr of aPackage.i : 0xbf9d6848 Addr of aPackage.j : 0xbf9d6848 Addr of aPackage.k : 0xbf9d6848 Addr of aPackage.f : 0xbf9d6848 Addr of aPackage.mathematicalEquation : 0xbf9d6848


Regards,

Dave
  #3  
Old 29-Apr-2008, 04:36
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 405
Peter_APIIT is on a distinguished road

Re: Prove Union has Single Address


Why u say aPackage is a pointer ?

As far as i know, pointer contains address of another variable.

Thanks for your help.
__________________
Linux is the best OS in the world.
  #4  
Old 29-Apr-2008, 07:21
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Prove Union has Single Address


Quote:
Originally Posted by Peter_APIIT
Why u say aPackage is a pointer

Look at the code. My code is the same as yours except for the cast.

The variable aPackage is a struct, so &aPackage is a pointer whose value is the address of aPackage.

In fact, it is not necessary to cast anything except the pointer to char. (Try my program again without the casts.) I just put the casts there for emphasis.

The "<<" operator for pointers to just about everything except pointer to char is overloaded to print the value of the pointer.

The "<<" operator for pointer to char is overloaded to treat a pointer to char as the address of a C-Style "string", so the cast is necessary in that case to make sure it prints the value of the pointer, and not try to print print a zero-byte terminated sequence of chars pointed to by that pointer.

Regards,

Dave
  #5  
Old 06-May-2008, 02:41
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 405
Peter_APIIT is on a distinguished road

Re: Prove Union has Single Address


I understand half of your explanation.

Why my union is contain a single char and not pointer to char but still need to cast it to pointer ?

Thanks.
__________________
Linux is the best OS in the world.
  #6  
Old 06-May-2008, 07:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Prove Union has Single Address


Quote:
Originally Posted by Peter_APIIT
Why my union is contain a single char and not pointer to char but still need to cast it to pointer
I didn't cast the union to a pointer.

Regards,

Dave
  #7  
Old 08-May-2008, 04:00
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 405
Peter_APIIT is on a distinguished road

Re: Prove Union has Single Address


Quote:
Why my union is contain a single char and not pointer to char but still need to cast it to pointer ?

What i mean is char and not char * ?
Why i need to cast since since i only have char inside the union?

Thanks.
__________________
Linux is the best OS in the world.
  #8  
Old 08-May-2008, 04:01
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 405
Peter_APIIT is on a distinguished road

Re: Prove Union has Single Address


Quote:
Why my union is contain a single char and not pointer to char but still need to cast it to pointer ?

What i mean is char and not char * ?
Why i need to cast since since i only have char inside the union?

Thanks.
__________________
Linux is the best OS in the world.
  #9  
Old 08-May-2008, 07:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Prove Union has Single Address


Quote:
Originally Posted by Peter_APIIT
What i mean is char and not char * ?
Why i need to cast since since i only have char inside the union?

Thanks.
You want to print the address of a char variable. The address of a char variable has type that is "pointer to char"

I tried to explain that the '<<' operator treats pointer to char like a C-style 'string'.

Here's an example:
CPP / C++ / C Code:
#include <iostream>

using namespace std;

int main()
{
    char c1 = '1';
    char *c2;
    c2 = "Hi";
    cout << "The '<<' operator is overloaded in such a way that"<< endl
         << "if you feed it a pointer to char, it thinks that" << endl
         << "you have a C-style 'string'" << endl << endl;
    
    cout << "c2 is a pointer to char." << endl
         << "The value of c2 is the address of the string " << endl
         << "literal, so the string is printed." << endl << endl;

    cout << "     c2: "
         << c2 << endl << endl; // The C-style "string is printed

    cout << "If we want to know the value of the pointer, c2," << endl
         << "then cast it to a pointer to void:" << endl;

    cout << "     c2: "
         << reinterpret_cast<void *>(c2) << endl << endl;

    // If you want use '<<' to print the address of a char variable
    // you have to tell the '<<' operator that you are not printing
    // a c-style string. To print the value of a pointer to char
    // you cast it to a pointer to void

    c2 = &c1;
    cout << "New the value of c2 is the address of a char. If" << endl
         << "we tried 'cout<<c2', the operator would try to print" << endl
         << "out a 'string', but there is no 'string' to print." << endl << endl;

    cout << "So, to print the value of the pointer, cast the variable" << endl
         << "to (void *):" << endl;

    cout << "     c2: "
         << reinterpret_cast<void *>(c2) << endl;

    return 0;
}

Output:
Code:
The '<<' operator is overloaded in such a way that if you feed it a pointer to char, it thinks that you have a C-style 'string' c2 is a pointer to char. The value of c2 is the address of the string literal, so the string is printed. c2: Hi If we want to know the value of the pointer, c2, then cast it to a pointer to void: c2: 0x8048aa0 New the value of c2 is the address of a char. If we tried 'cout<<c2', the operator would try to print out a 'string', but there is no 'string' to print. So, to print the value of the pointer, cast the variable to (void *): c2: 0xbfd692df

Regards,

Dave
  #10  
Old 10-May-2008, 01:46
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 405
Peter_APIIT is on a distinguished road

Re: Prove Union has Single Address


Not really understand what u talking here.

But my understand is like this :

CPP / C++ / C Code:
cout << "\n" << "Address of I : " << &aPackage.i;

When i put execute this statement, my intention is to print the address of the char but << operator treat this as pointer to char, in the meanwhile << operator treat this to C style string.

Therefore, i need dereference to get the address of char.

For instance, u declare int a;

but you pass it as (&a); you also can treat this as pointer in function definition.

I think i understand(May be).

By the way, thanks for your help.
__________________
Linux is the best OS in the world.
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked List demo using a single pointer Howard_L C Programming Language 18 09-Jul-2007 23:11
a tester class and then some. postage Java Forum 1 06-May-2006 15:48
[Tutorial] How to hide e-mail address from SPAM Bots BobbyDouglas Web Design Forum 40 13-Feb-2006 09:44
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:21.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.