Structures

C Tutorials

It has been already studied that arrays are used to represent a group of data items of same data type. However it cannot be used to combine data of different data types.

For this reason only, the concept of STRUCTURES comes into picture. It is mechanism of packaging data of different types.

For example:-

If you need to store ADDRESSES of different people, one needs variable for storing House Number(int), Society(char array), State, Contact number(Long int) etc.

 

This structure would be like as:-

 

Struct  address

{

Int House_no;

Char society[20];

Char state[10];

Long int contact;

};

 

 

 

GENERAL SYNTAX OF STRUCTURES

 

Struct    tag_name

{

Data type   member1;

Data type   member2;

------------   --------------;

------------   --------------;

};

 

Note: - The template is terminated by Semicolon.

 

 

 

Ways of DECLARATION of STRUCTURE VARIABLES

Normal Declaration:-

Structures are basically USER DEFINED DATA TYPE. So there declaration will be like the declaration of Fundamental Data type.

Declaration of variables for above example will look like:-

Struct  address  student1, student2, student3;

This declares three variables of type”struct address” which will store the addresses of three students.

Point to remember: -

  • Structure members do not require memory space until and unless structure variables such as student1, student2 etc. are not declared. After declaration only, the memory is allocated to each variable member.

Declaration at the time of Definition

Structure variable can be declared at the time of definition also.

 

Struct  address

{

Int House_no;

Char society[20];

Char state[10];

Long int contact;

} student1,student2,student3;

 

 

Type-defined structures

Keyword typedef can also be used in structure definition.

Typedef struct

{

Data type   member1;

Data type   member2;

------------   --------------;

------------   --------------;

 

} type_name;

 

Type_name can now be used to declare struct variables.

 

Type_name variable1, variable2,.............;

 

 

How to access Structure Members?

 

As discussed earlier members are not variables. They should be linked to the structure variables in order to make them meaningful members.

 

For example:- to assign a “house_number”, we must be knowing the name of the student about whose “house_number” we are talking about.

 

So to access the “house_number” of a variable say student1 we need to write:-

 

Student1.house_number=18;

Strcpy(student1.state,”New York”);

 

Here “.” is called the ‘dot operator’ or ‘period operator’.

 

Points to remember:-

  • One structure variable of a particular structure can be copied into another structure variable of the same structure type only as shown below:-

student2 = student1;

                By this statement we are copying the address of student1 in student2.

  • However comparison of structure variables is not allowed in C.

Statements like:-

Student1==student2;

Student1!-student2;

are strictly invalid.

 

 

A sample program -

 

 

#include<stdio.h>

#include<conio.h>

 

struct student

{

int roll_no;

char name[20];

int class_rank;

}student1,student2;

 

void main()

{

clrscr();

 

printf("Please Enter the Name Of the Student:- ");

gets(student1.name);

 

printf("Please Enter the Roll Number Of the Student:- ");

scanf("%d",&student1.roll_no);

 

printf("Please Enter the Class Rank of the Student:- ");

scanf("%d",&student1.class_rank);

 

student2=student1; /* Assigning the specifications of student1 into student2*/

 

printf("\n\n%s, Roll Number %d has Secured Rank %d in Class",student2.name,student2.roll_no,student2.class_rank);

 

getch();

}  

 

 

Output will be like this -

 

Output of the sample program

 

 

Download source code.

 

 

UNIONS

 

Unions are quite similar with structures. Syntax of both is exactly same. The only difference in the concept of structures and unions is that in structures every member variable is assigned separate memory space whereas all the members of a union use the same memory space.

This implies that a union can contain data for only one variable at a time.

 

 

SYNTAX

Union   sample

{

Int x;

Float y;

Char z;

} variable1;

 

This code declares a variable named “variable1” of type union sample. The compiler allocates some space in the memory. This space is large enough to hold the largest variable type in the defined union. Here float is the largest data type requiring 4 bytes (the size of float data type on my compiler, may be different on yours).

 

  • Note: Different compilers may assign different memory space for the same data type.
  • Union members are accessed in same way as structure member is accessed.