![]() |
|
#1
|
|||
|
|||
Prove Union has Single AddressHello 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:
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
|
|||
|
|||
Re: Prove Union has Single AddressQuote:
CPP / C++ / C Code:
Output (from GNU g++) Code:
Regards, Dave |
|
#3
|
|||
|
|||
Re: Prove Union has Single AddressWhy 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
|
|||
|
|||
Re: Prove Union has Single AddressQuote:
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
|
|||
|
|||
Re: Prove Union has Single AddressI 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
|
|||
|
|||
Re: Prove Union has Single AddressQuote:
Regards, Dave |
|
#7
|
|||
|
|||
Re: Prove Union has Single AddressQuote:
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
|
|||
|
|||
Re: Prove Union has Single AddressQuote:
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
|
|||
|
|||
Re: Prove Union has Single AddressQuote:
I tried to explain that the '<<' operator treats pointer to char like a C-style 'string'. Here's an example: CPP / C++ / C Code:
Output: Code:
Regards, Dave |
|
#10
|
|||
|
|||
Re: Prove Union has Single AddressNot really understand what u talking here.
But my understand is like this : CPP / C++ / C Code:
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 GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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