Home » questions » tween structure and union in c laguage?

tween structure and union in c laguage?

2006-08-02 03:11:28, Category: Programming & Design

Answers

  1. griz803

    On 2006-08-02 11:27:08


    The other answer is correct, as far as it goes. Important points about a union: 1) A union only allocates as much memory as its largest element (member) requires. 2) The union will store one and only one actual value for one element at a time. If another element is stored before the first is retrieved, the first stored value is lost. This presents the programmer with extra responsibility when using a union to determine that the logic will properly store all values used in the union through a correct lifetime and scope. Important points about a structure: 1) A structure allocates the total size of all elements in it. 2) Members inside a structure are always stored in separate memory locations throughout the lifetime and scope of the entire structure. Manipulations of one member will not affect the values of any of the others in any way unless they are operated on in code to do so. Similarities: 1) They both can accept the dot (.) operator to address a member from the object name, as struct.member or union.member. 2) They both use brace delimited declarations to create the template for the data object. Both accept tagname and name as well as explicit initialization as options. 3) They both can have their size correctly determined as maximum size in bytes by use of the sizeof() operator. An example program is shown below. I hope it helps you understand. #include #include /* Declares union */ union one{ char one; int two; float three; }; /* Declares structure */ struct two{ char one; int two; float three; }; int main(void) { /* Uses tag names to create structure S and union U. */ struct two S; union one U; /* Outputs object sizes to screen. */ printf("%d is the size of S, as structure.\n", sizeof(S)); printf("%d is the size of U, as union.\n\n", sizeof(U)); /* Loads values into S and U, as below. */ S.one = 'A'; S.two = 3645; S.three = 678.32; U.one = 'A'; U.two = 3645; U.three = 678.32; /* Echos values to screen, so conclusions are easier. */ printf("The values 'A', 3645 and 678.32 have been loaded" "in structure S members and\n union U members both.\n\n"); /* Shows values now in S members. */ printf("%c is equal to the value S.one\n", S.one); printf("%d is equal to the value S.two\n", S.two); printf("%3.2f is equal to the value S.three\n\n", S.three); /* Shows the value now stored as corrupted and good. */ printf("%c is equal to the value U.one\n", U.one); puts("Oops! Doesn't look like an 'A'!\n"); printf("%d is equal to the value U.two\n", U.two); puts("Oops! Doesn't look like 3645 either!\n"); printf("%3.2f is equal to the value U.three\n\n", U.three); /* Non-pertinent stuff for running under Windows and stuff */ system("PAUSE"); return 0; }
  2. ♥HotIce♥

    On 2006-08-02 03:59:11


    I suppose you've asked for difference between Structure and Union . Well The difference between structure and union in c are: 1. union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members. 2. In union, one block is used by all the member of the union but in case of structure, each member have their own memory space For more details : http://www.faqfarm.com/Q/What_is_the_difference_between_a_union_and_a_structure_in_C http://www.gidforums.com/t-5590.html Hope this helps .