Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Structures And Unions

Posted By: Lurleen Fischer     Category: C Programming     Views: 10344

This article explains about Structures and Functions, Unions, Structure of Bitfields etc...

Structures And Unions

  • The need to have heterogeneous data to be stored together like student’s data
  • It is analogous of record of certain kind
  • It can group logically related items to make the complex design more meaningful
  • Structure and unions are two different data types which hold other data types in turn

Structure Definition


Examples

 struct student { int r_no;
char name[20];
char address[20];
                        } stud1,stud2,stud3;
 struct     { int r_no;
char name[20];
char address[20];
                        } stud1,stud2,stud3;

Assigning Values

 stud1.r_no = 10;
 strcpy(stud1.name , “Ram” );
 strcpy (stud1.address , “Ayodhya”);
 scanf(“%d”,&stud2.r_no); 
 scanf(“%s”,stud2.name);
 static struct student { …} stud3 = {20, ”Bharat” ,”Ayodhya”}
 static struct student student4= {30, “Shatrughna”, “Ayodhya”}
 struct student{…} stud1 = {40, “Laxman”, “Ayodhya” } // No static!
 main()
 { struct student student2 = {…}
 }

Note : C does not support initialization to the template variables, it must be done to actual variables.

Comparing Structures

Operation Meaning
person1 = person2 Assign person2 to person1
person1 == person2  Compare all members of person1 and person2 and return 1 if they are
equal and 0 otherwise
person1 != person2 Return 1 if all the members are not equal, zero otherwise
  • Array of Structures
  • Array in structures
  • Structures within structures
  • The hierarchy of nested structures and use of ‘.’ operator to address deepest member
  • The inner structure can have more than one variables!


Structures and Functions

  • All individual members of the structure can be passed as if individual variables
  • Whole structure can be passed to a function
  • The function, in the second case, must have the structure argument defined to accept 
  • A pointer to structure can be passed like an array, where a function can work on the elements directly


Unions

  • Unions are structures with storage for a single member only at a  time
  •  union item{ int m; float x; char c;} code;
  •  code.m, code.x, code.c (can’t co-exist!)

sizeof() Operator

  •  sizeof(variable) or sizeof(type of variable)
  •  sizeof(student) or sizeof stud1
  •  Summation of the size of all individual members is not always equal to  the size of structure
  •  Sizeof( <array name>)/sizeof(record) will give total actual elements in  the array

bitfields

  • Integer fields are too big for one or two bit data which is common in networking 
  • That is also very useful in data handling for files
  • A bit field is a set of adjacent bits whose size can be 1 to 16 bits
  • A word can be divided in no of bit fields
  • The internal rep depends on the machine


Structure of Bitfields

  • The first field always starts from the first bit of the word
  • A bit field can not overlap integer boundaries, total length of all bitfields of a structure should be < word size
  • Otherwise the overlapping field is forced to start with beginning of the next word
  • Unnamed fields provides padding
  • There can be unused bits in the word
  • Address of a bit field variable can not be taken, i.e. scanf can not be used to read it
  • We can’t use pointers to access them
  • Bit fields can not be arrayed
  • If values are assigned outside the range, results are unpredictable 

Examples of Usage

 emp.sex = 1;
 emp.age = 50;
 scanf(“%d”,AGE); emp.age = AGE;
 sum = sum + emp.age
 if (emp.marital_status) …
 printf(“%d”,emp.age);

 Normal structure elements can be combined with bit fields
 packing will be done for cons BFs only

 struct personal
{ char name[20];
struct addr address; // structure variable
   unsigned sex :1;
  …… } emp [100];
 struct pack
 { unsigned a:2;
    int count;
    unsigned b :3; }
  
Share: 

 
 
 

Didn't find what you were looking for? Find more on Structures And Unions Or get search suggestion and latest updates.

Lurleen Fischer
Lurleen Fischer author of Structures And Unions is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!