Chapter 14

 

Structures

 

C provides data type called structure that allows us to group a number of data types together. Each group may contain a number of same / different data type.

 

//program to show the use of structure data type

#include<stdio.h>

#include<conio.h>

void main( )

{                                                                                                                                                                                                                                  OR

struct student                            struct student               

{                                         {

char name[20];                            char name[20];

int roll;                                 int roll;

float fee;                                float fee;

};                                        }s1,s2;

struct student s1,s2;

 

 

printf(“Enter name, roll no and fee of student 1\n”);

scanf(“%s\t %d\t %f”,s1.name,&s1.roll,&s1.fee);

printf(“Enter name, roll no and fee of student2\n”);

scanf(“%s\t %d\t %f”,s2.name,&s2.roll,&s2.fee);

printf(“Name: %s roll: %d Fee:%f”,s1.name,s1.roll,s1.fee);

printf(“Name: %s roll: %d Fee:%f”,s2.name,s2.roll,s2.fee);

 

getch();

}

 

 

 

 

 

 

 

 

 

 

Declaration of a structure-

 

      struct student

{

char name[20];

int roll;

float fee;

};

 

Here, closing brace is followed by semi colon which terminates the structure declaration. This statement defines a new data type called struct student.

Once the new structure data type has been defined, one or more variables of the same type can be declared.

e.g. struct student s1,s2;

This statement allocates space in memory for s1 and s2 variable.

The elements of s1 and s2 variable are accessed as below.

            s1.name           s1.roll  s1.fee

            s2.name           s2.roll  s2.fee

 

 

Array of structure

 

It is possible to define an array of structures; i.e. an array in which each element is a structure.

 

//program to demonstrate the use of array of structure

 

struct student

{

char name[20];

int roll;

float fee;

};

 

struct student s[3];

int i;

for(i=0;i<3;i++)

{

printf(“Enter name , roll and fee for student %d\n”,i+1);

scanf(“%s %d %f”, s[i].name, &s[i].roll, &s[i].fee);

}

printf(“name\troll\t Fee\n”);

for(i=0;i<3;i++)

{

printf(“%s\t %d\t %f\n”, s[i].name, s[i].roll, s[i].fee);

}

}

 

 

#Structure within structure

 

The individual members of a structure can be other structures as well. We now include a new member called date which itself is a structure. There are two ways of declaring such structure.

 

struct date

{

int day;

int month;

int year;

};

 

struct student

{

char name[20];

int roll;

struct date dob;

float fee;

};

 

struct student s[3];

int i;

for(i=0;i<3;i++)

{

printf(“Enter name, roll and fee of student %d”,i+1);

scanf(“%s %d %f”, s[i].name, &s[i].roll, &s[i].fee);

printf(“Enter date of birth\n”);

printf(“Enter day, month and year of student %d\n”,i+1);

scanf(“%d %d %d”,&s[i].dob.day, &s[i].dob.month, &s[i].dob.year);

}

printf(“Name \t roll \t Fee \t DOB \n”);

for(i=0;i<3;i++)

{

printf(“%s\t %d\t %f\t %d\t %d\t %d\t\n”,s[i].name, s[i].roll, s[i].fee, s[i].dob.day, s[i].dob.month, s[i]..dob.year);

}

}

 

 

#Pointers to structure

 

Pointers can be used with structures. We can create a pointer to structure in the same way as we create a pointer to any other data type.

 

 

struct student

{

char name[20];

int roll;

float fee;

};

struct student *sptr;

 

 

Accessing members of a structure with a pointer can be done with the asterisks operator as;

(*sptr).name;

(*sptr).roll;

      (*sptr).fee;

 

Because the dot operator has precedence over the asterisks operator, we use parenthesis to override the precedence rules.

Note: There is another notation which means the same as (*sptr), and looks simpler. It uses -> (a minus sign followed immediately by the greater sign symbol).

 

sptr->name                  is same as (*sptr).name

sptr->roll                     is same as (*sptr).roll

 

 

//program to illustrate the use of pointer structure.

 

struct student

{

char name[20];

int roll;

float fee;

};

struct student s,*sptr;

sptr=&s;

printf(“Enter name, roll and fee of student”);

scanf(“%s%d%f”,&(*sptr).name, ”,&(*sptr).roll, ”,&(*sptr).fee);

}

 

 

Structure and function

 

To use the structure within the user defined function also, we have to declare the structure above the main (). So it will work as global structure.

 

//program to illustrate the use of structure and function

 

struct date

{

int day;

int month;

int year;

};

 

struct student

{

char name[20];

int roll;

struct date dob;;

float fee;

}s1;

void scan_structure();

void print_structure();

void main()

{

scan_structure();

print_structure();

}

void scan_structure()

{

printf(“Enter name, roll and fee of student”);

scanf(“%d%d%d”,&s1.name,&s1.roll,&s1.fee);

printf(“Enter day, month and year of student”);

scanf(“%d%d%d”,&s1.dob.year,&s1.dob.month,&s1.dob.year);

}

void print_structure()

{

printf(“Name \t roll\t fee\t DOB\n”);

printf(“%s\t%d\t%f\t%d/%d/%d\n”,s1.name,s1.roll,s1.fee,s1.dob.day,s1.dob.month,s1.dob.year);

}

 

User defined data types (typedef)

 

In C, the data type of any identity can be customized by the user. typedef is the keyword, which enables users to define new data type equivalent to existing data types.

 

e.g.

typedef int age;

age male female;              ~~int male, female;

 

typedef struct

{

int month;

int day;

int year;

}date;

Union

 

      Structure enables us to treat a number of different variables stored at different places in memory; a union enables us to treat the same space in memory as a number of different variables.

Syntax for unions is identical to that of structures, except that the keyword struct is replaced with the keyword union.

#Difference between structures and unions:

The amount of memory required to store a structure variables is the sum of size of all the members. In union, the amount of memory required is the same as members that occupies largest memory.

e.g.

struct student

{

char student[20];

int roll;

float fee;

};

void main()

{

struct student structuremem;

union student1 unionmem;

printf(“The size of structure is:%d”,sizeof(structmem));

printf(“The size of union is:%d”,sizeof(unionmem));

}

 

//program to show examples of union

 

#include<stdio.h>

union student

{

 char name[20];

int roll;

float fee;

};

void main()

{

union student std;

strcpy(std.name,”Bibek”);

std.roll=121;

std.fee=2500.00;

printf(“The name is:%s \n”, std.name);

printf(“The roll is:%d”, std.roll);

printf(“The fee is %f”, std.fee);

}

 

 Full Width

 


Post a Comment

0 Comments