Exforsys

Home arrow Technical Training arrow C Tutorials

C Programming - Data Types : Part 2

Page 1 of 4
Author: Exforsys Inc.     Published on: 21st Aug 2011

Structure

A struct is an aggregate type, meaning that it is made up of other objects. The objects that make up a struct are called its members. The members of a struct may be accessed by the '.' or '->' operators. A struct may be named or unnamed. Let's look at some examples:

Ads

Sample Code
  1. #include <stdio.h></stdio.h>
  2.   
  3.    struct person
  4.    {
  5.            char first[100];
  6.            char last[100];
  7.            int  age;
  8.            struct {
  9.                    char addrline[500];
  10.                   char city[100];
  11.                   char state[30];
  12.                   char zip[15];
  13.           } address;
  14.   };
  15.  
  16.   void printperson( struct person *p )
  17.   {
  18.           printf( "%s %s is %d years oldn", p->first, p->last,
  19.             p->age );
  20.   }
  21.  
  22.   void main()
  23.   {
  24.           struct person a;
  25.           struct person b[100];
  26.  
  27.           strcpy( a.first, "Bob" );
  28.           strcpy( a.last, "Smith" );
  29.           a.age = 33;
  30.           strcpy( a.address.addrline, "123 Main Street" );
  31.           strcpy( a.address.city, "St. Louis" );
  32.           strcpy( a.address.state, "Missouri" );
  33.           strcpy( a.address.zip, "63141" );
  34.  
  35.           b[7] = a;
  36.           printperson( &b[7] );
  37.   }
Copyright exforsys.com


The output is:

Bob Smith is 33 years old

Lines 3-14 declare a named structure called person. Its members are two character arrays first and last, an integer age and a nested unnamed struct. The nested unnamed struct is refered to by the identifier address.

Line 24 declares a to be a struct person, and line 25 declares an array of struct persons of size 100. Lines 27 - 33 show how to access the members of a struct using the '.' operator. Line 35 shows that it is possible to use the assignment operator with structs - each member of a is copied to the corresponding member of b[7]. Line 36 calls printperson() with a pointer to the 8th element of b.

The printperson() function on lines 16-20 show accessing a struct's members through the '->' operator when you have a pointer to a struct.

Structures will be discussed more in another tutorial.

Unions

A union is similar to a struct except that only one of the members may have a value at one time. Unions are declared the same as structs with the 'union' keyword replacing 'struct', and its members are accessed with the same '.' and '->' operators. Also, like structs, they can be named or unnamed and can be nested.

Since a union can only have one of its members holding a value at one time, it only needs enough memory to hold its biggest member. It can use that space for all of its members.

Here is an example comparing structs and unions:

Sample Code
  1. #include <stdio.h></stdio.h>
  2.   
  3.    struct a
  4.    {
  5.            int num;
  6.            float flt;
  7.    };
  8.   
  9.    union b
  10.   {
  11.           int num;
  12.           float flt;
  13.   };
  14.  
  15.   void print_a( struct a * ap )
  16.   {
  17.           printf("a.num = %d, a.flt = %fn", ap->num, ap->flt );
  18.   }
  19.  
  20.   void print_b( union b * bp )
  21.   {
  22.           printf("b.num = %d, b.flt = %fn", bp->num, bp->flt );
  23.   }
  24.  
  25.   void main()
  26.   {
  27.           struct a a;
  28.           union b b;
  29.  
  30.           printf( "size of struct a = %d bytesn", sizeof( a ) );
  31.           printf( "size of union b = %d bytesn", sizeof( b ));
  32.  
  33.           a.num = 9;
  34.           a.flt = -45.73;
  35.  
  36.           b.num = 9;
  37.           b.flt = -45.73;
  38.  
  39.           print_a( &a );
  40.           print_b( &b );
  41.  
  42.           b.num = 9;
  43.           print_b( &b );
  44.   }
Copyright exforsys.com


The output of this program is:

size of struct a = 8 bytes size of union b = 4 bytes a.num = 9, a.flt = -45.730000 b.num = -1036588155, b.flt = -45.730000 b.num = 9, b.flt = 0.000000

Lines 3-7 declare a struct a that is has integer and float members. Lines 9-13 declare a union that is composed of the same types of members. However, from the output produced from lines 30-31 you can see that the union takes only 4 bytes while the struct takes 8. That is because the struct needs space for both the int (4 bytes) and float (another 4 bytes). The union can only hold either an integer value or a float value at one time, so it only needs 4 bytes total. The same 4 bytes in memory are used to hold both members.

Lines 33-34 assign values to the members of the struct a and line 39 prints out the values of the struct. Both values are printed correctly.

Ads

Lines 36-37 assign values to the members of the union b and line 40 prints out those values. However, notice that only the b.flt value is correct. That is because the union only holds one value at a time, and the last member assigned was b.flt. On line 42 b.num is assigned and the values printed again. This time b.num has the correct value while b.flt does not.

There will be more information about structs and unions in another tutorial.

 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments