Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Union and struct

Status
Not open for further replies.

ndogg

Programmer
Feb 7, 2000
156
US
I'm still a newbie at C programming (admittedly, I should have started with this rather than starting with JavaScript).&nbsp;&nbsp;I'm having a hard time understanding the difference between the <FONT FACE=monospace>struct</font> and <FONT FACE=monospace>union</font> keywords.&nbsp;&nbsp;I read this somewhere and it's what confused me: <i>The syntax to define a union is simular to the struct keyword</i><br><br>If anyone can explain this to me more clearly, it would be greatly appreciated. <p>REH<br><a href=mailto:hawkdogg@crosswinds.net>hawkdogg@crosswinds.net</a><br><a href= by Linux</a><br>Learn Linux and Leave out the Windows :)
 
Unions and structs DO look alike but are totally different!<br>Unions basically save memory by allowing two or more different variables to share the same memory location. They can also be used to convert data types, but it's not recommended due to poor portability and being prone to bugs.<br>Structs basically provide a mechanism in which to organize data into logical groups. The members of a struct are totally independant and do NOT share memory locations.<br> <p> <br><a href=mailto:Kim_Christensen@telus.net>Kim_Christensen@telus.net</a><br><a href= Page</a><br>
 
As Zbuilder said, Union may have several members. But they share one memory only . Now you may ask what memory , how much memory etc....&nbsp;&nbsp;For eg.<br><br>union ABC {<br>char A;<br>char B[10];<br>int C;<br>float D;<br>};<br><br>Now union ABC will be assigned 10 bytes of memory only. Becuase the variable B[10] is having the largest size among all the variables. Integer&nbsp;&nbsp;C takes 2 or 4 bytes (depending upon the OS). float D takes 4 bytes , char A takes 1 byte. Now each variable will be accessing the same memory area of 10 bytes depending upon their size. <br><br>Does this answer your question ?<br>Thanx<br>Siddhartha Singh<br><A HREF="mailto:ssingh@aztecsoft.com">ssingh@aztecsoft.com</A>
 
If union should not be used to change data types, then what? <p>REH<br><a href=mailto:hawkdogg@crosswinds.net>hawkdogg@crosswinds.net</a><br><a href= by Linux</a><br>Learn Linux and Leave out the Windows :)
 
There are two points <br>1) Either let the compiler do the implicit data conversion. For eg. if you assign a float to an integer the decimal part is truncated.<br><br>2) You can use Typecast method to explicitly cast the data type.<br><br>&nbsp;For eg.<br>int i = 0; <br>&nbsp;char ch = char(i);// typecast<br><br>Does this answer your question ?<br>Thanx<br>Siddhartha Singh<br><A HREF="mailto:ssingh@aztecsoft.com">ssingh@aztecsoft.com</A>
 
You can use a union to convert data types, but like I said it's not very portable and makes for harder to read code. Also, to properly do this, you need to turn off the boundry alignments in your compiler. This is because most compilers will align data on a set boundary by default. For example:<br><br>union Alphabet { char A[5];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long B;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br><br>Might align up in memory like this (depends on processor):<br><br>Memlocation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byte<br><br>1200 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byte from previous variable<br>1201 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(char A string position 0)<br>1202 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(char A string position 1)<br>1203 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(char A string position 2) & (long B byte 1)<br>1204 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(char A string position 3) & (long B byte 2)<br>1205 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(char A string position 4) & (long B byte 3)<br>1206 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(char A string position 5) & (long B byte 4)<br><br>Hope this clears it up for you. <p> <br><a href=mailto:Kim_Christensen@telus.net>Kim_Christensen@telus.net</a><br><a href= Page</a><br>
 
zBuilder, can you be more explicit about the convertion thingy?
 
Check the dates people!
4 years and 4 months have passed.


--
 
hi,
i m a beginner in C & i hav a doubt regarding structures.
the code i tried is:
/* code beginning */
struct s{
int i;
char c;
}m = {1,'A');//if any syntax errors... sry

main() {
struct s *p;
printf ("%d, %c, %d\n", *p, p->i);
}
/* code end */

o/p: 1 A 1
/***********************/

my doubt is how a struct members are stored on to the stack..
why i get this o/p?

thnx,
 
96gfeeinte: Please post new topics in a new thread. (Or, failing that, at least one that's not a zombie.) Otherwise, you probably won't get a reply.


You should not be getting that output from that code. You would probably get a "1" followed by either a program crash or a random string of characters.


Again, please post this as a new topic. Do the same thing you did to post this one, but do it from the main page of this forum. That's the one that lists thread topics.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top