Reading:  

Getting started with C Language


Structure and union in C

Structure and union

Structures

A structure is a tool for handling a group of logically related data items. Arrays only handle similar data items where the can be used to represent a set of attributes, such as a student name , roll name , mark , address. The structure may call as a record in many other languages. It helps to organize complex data in a more meaningful way. It is often used in program design to store logically related data thus it is a powerful tool or concept of C language.

The keyword struct declares a structure to hold the details of its fields. The field is called structure members or structure elements.

Declaration of a structure

Syntax :                                    

struct structure name
{
   Structure element1;
   Structure element2;
   Structure element3;
  .          
.
.
.
Structure element n;
};;

Example :

struct book
{
   char title [20];
   char author [15];
   int page;
   float price;
};;

Here book is a structure with fields title , author , page , and its price. The above declaration has not declared any variable. It simply describes a format called template to represent information as shown below

Structure book

 

Array of 15 characters

Array of 20 characters  

Float

Integer

 

We can declare structure variables using the tag name any where in the program.

Syntax :

struct structure name structure variable1, structure variable2, structure variable3,………….. structure variable;

Example :

struct book book1, book2;

defining a structure we must note the following :

  • The template must terminated with a semicolon
  • While the entire declaration is considered as a statement, each member is declared independently for its name and type in a separate statement inside the template.
  • The tag name such as book can be used to declare structure variable of its type later in the program.

 Also can combine both template declaration and variable declaration in one statement like

struct book
{
char title [20];
char author [20];
int page;
float price;
} book1, book2, book3;

Giving values to members

We can assign values to the members of a structure in many ways . the members itself are not variables. They should be linked to the structure variables in order to make them meaningful members. The link between a member and a variable is established using the member pointer operator ‘.’ Which is also called dot operator.

For example :

book1.price

it is a variable representing the price of book1 and can be treated as any other ordinary variable.

To give values we can use

book1.price=250.00;

Here the value 250.00 assigned as the price of first variable.

To take values from the keyboard we can use the way

scanf(“%f”,&book1.price);

#include
struct book
{
   char title [20];
   char author [20];
   int page;
   float price;
}book1;
book1 = {"let us C",”Jas Chahal”,300,250};
void main()
{
 printf("The book details are: );
 printf("%s %S %d %f”, book1.title,book1.author, & book1   .page, & book1.price);   
}

OUTPUT

The book details are:

 let us C , Jas Chahal , 300 , 250.00

Size of structure

We can calculate the size of structure by adding the size of the structure members and also the special operator size of is used to find the size.

Adding the structure members size

This is done by observation, because if we know the size of the members add them all up to find the size. For example

struct book
{
char title [20];
char author [20];
int page;
float price;
};

Here we find the size by adding the member sizes

Title = char of 1 byte and array size 20 then 20 bytes.

author = character array of 15 bytes

page = integer of 2 bytes

price = float of 4 bytes

structure size = 20 + 15 + 2 + 4 =41 bytes

Using size of operator

Size of operator is used to calculate size of a data type, variable or expression. It is similar to a function.  

#include<stdio.h>
 struct book
 {
   char title[20];
   char author[15];
   int pages;
    float price
}book1;
book1 = {"let us C",”Balaguruswami”,300,250};
void main()
{
    printf("Size of Structure : %d",sizeof(book1));
}

Output :

Size of Structure : 41                                    

Operations on structure variables

Two variables of the same structure type can be compared as a normal variable comparison. As book1 and book2 are  belongs to same structure and then we can do following operations.

book1 = book2           

it assign the details of book2 to book1

book1 = = book2

it is a comparison operator it compare all the members of book1 and book2 and return 1 if they are equal or return 0 if they are not equal.

book1 ! = book2         

It  is also a comparison operator it compare all the members and return 1 if all the members are not equal otherwise return 0.

 

Array of structures

It  means we can declare an array of structures where each element of the array representing a structure variable.

Syntax :

struct structure name
{
Structure element1;
Structure element2;
Structure element3;
.
.
.
.
Structure element n;
};

struct structure name array of structure variables;

Example :

struct marks
{
int eng;
int maths;
int science;
};
struct mark student[3];

To give values to members we need

student[0].eng=45;
student[0].maths=34;
student[0].science=40;
student[1].eng=23;
.
.
.
student[2].science=33;

Structure within structure

Structure within structure also called nesting of structure. It is permitted in c language.

Example :

struct student
{
char name[20];
int rollno;
struct dob
{
int day;
char month[10];
int year;
};
char class[20];
};

Union

The concept of union is almost same as a structure , thus the syntax is also similar. The difference between union and structure is in storage. In structures each member has its own storage location but in union the members share the same location.

Syntax :

union union name
{
Union members
};

Example :

union product
{
int no;
char c;
float price;
} code;          

 

Here the union contain three members each of different data type. The biggest in size is float of four bytes.

1001 1002 1003 1004

 

Here is the 4 bytes and union it share integer character and float within this 4 bytes. Thus this union use only 4 bytes for these three members.

If it was a structure

struct product
{
int no;
char c;
float price;
} code;         

It need location for its each members thus size of the union is 4 bytes and size of the same structure is 4 bytes for float +  2 for integer and 1 for the character. So total 4 + 2 + 1=7 bytes needed.

 

 

  

 

 

 

Description

This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial



Environment

We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.

Audience

Programmers new to C Language

Learning Objectives

Learn basics of C language and build from that knowledge.

Author: Subject Coach
Added on: 3rd Jan 2015

You must be logged in as Student to ask a Question.

None just yet!