C-Programming

How to declare the array of structure?

First of all let us know what is array of structure. As all of us know that the array is collection of different data of same type that sharing a common name which is located in the continuous memory location. In the simple array there is only one type data, but if we have to store different type of data then we use structure. Now the collection of same type of structure that shares a common name is array of structure. It can be declared as the combination of structure and array. For example:

          struct student

{

int roll;

char name[25];

char address[25];

float mark;

};

          struct student std[10];

OR

          struct student

{

int roll;

char name[25];

char address[25];

float mark;

}std[10];

Here both the declarations are to declare the same type of declaration for different 10 students. The entire student variable contains roll, name, address and marks as member variables.

          We can access the value of member variables of array of structure by using the same syntax same as the above used to access the member variable of simple structure. This program illustrates how to access the member variable of array of structure:

Write a program that reads the name and roll number of 10 students and display using structure.

#include<stdio.h>

#include<conio.h>

void main()

{

struct student

{

char name[20];

int roll;

}std[10];

int i;

//loop for reading the name and roll number of the 10m students

for(i=1;i<=10;i++)

{

    printf|(“\nplease enter the name of the %dth student:\t”,i);

    scanf(“%s”,std[i].name);

    printf(“\nenter the roll number:\t”);

    scanf(“%d”,&std[i].roll);

}

//loop for displaying the name and roll number

for(i=1;i<=10;i++)

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

getch();

}

Write a program that reads the name, and address of ‘n’ students and arrange them into ascending order on the basis of name using structure.

#include<stdio.h>

#inlcude<conio.h>

void main()

{

struct

{

char name[25];

char add[20];

}std[50];

int n,i,j;

char temp[25];

printf(“\nplease enter the number of students:\t”);

scanf(“%d”,&n);

printf(“\n\nreading the data of the students.\n\n”);

for(i=1;i<=n;i++)

{

printf(“\nplease enter the name of the %dth student:\t”,i)

scanf(“%s,std[i].name);

printf(“\nplease enter the address of the student:\t”);

scanf(“%s”,std[i].add);

}

//sorting

for(i=1;i<=n;i++)

for(j=i;j<=n;j++)

    {

if(strcmp(std[i].name,std[j].name)>0)

         {

strcpy(temp,std[i].name);

strcpy(std[i].name,std[j].name);

strcpy(std[j].name,temp);

strcpy(temp,std[i].add);

strcpy(std[i].add,std[j].add);

strcpy(std[j].add,temp);

         }

printf(“\n\n\ndata after arranging into the ascending order:\n\n”);

printf(“Name\t\tAddress”);

for(i=1;i<=n;i++)

{

printf(“\n1. %s\t%s”,std[i].name,std[i].add);

}

getch();

}

 

Typedef

C provides a facility called typedef (type definition) that allows users to define an identifier that would represent an existing data type. The syntax of this function is :

typedef type identifier;

Here type refers to an existing data type and “identifier” refers to the “new” name given to the data type. The existing data type may belong to any class of type, including the user defined ones. Remember that the new type is’ new’ only in name, but not the data type. typedef can not crate a new type.

For example, the declaration

   typedef int Length;

makes the name Length a synonym for int. The type Length can be used in declarations, casts, etc., in exactly the same ways that the int type can be:

   Length len, maxlen;

   Length *lengths[];

Similarly, the declaration

   typedef char *String;

makes String a synonym for char * or character pointer.

We can use the typedef() function in structure in similar manner as in the other simple programmer. This can be understood by the following example:

Write a program to create a structure using typedef() statement containing members to store name, roll number and marks on ten subjects of 20 students. Display the name, roll number and average marks of a student having highest average mark.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

typedef struct student

{

char name[25];

int roll, marks[10];

int total=0;

float ave;

};

int i,j;

student std[20];

float temp;

cahr temp1[20];

int temp2;

for(i=1;i<=20;i++)

{

printf(“\nenter the name of the %dth student:\t”);

scanf(“%s”,std[i].name);

printf(“roll number=\t”);

scanf(“%d”,&std[i].roll);

for(j=1;j<=10;j++)

{

printf(“\nenter the marks on %dth subject:\t”);

scanf(“%d”,&std[i].marks[j]);

std[i].total+=std[i].marks;

}

std[i].ave=(float)total/10;

}

for(i=1;i<=20;i++)

     {

for(k=i+1;k<20;k++)

          {

if(std[i].ave<std[k].ave)

          {

temp=std[i].ave;

std[i].ave=std[k].ave;

std[k].ave=temp;

strcpy(temp1,std[i].name);

strcpy(std[i].name,std[k].name);

strcpy(std[k].name,temp1);

for(j=1;j<=10;j++)

     {

temp2=std[i].mark[j];

std[i].mark[j]=std[k].marks[j];

std[k].marks[j]=temp2;

     }

}

printf(“\n\n\nthe mark sheet of the student having average mark is given :\n”);

printf(“\nname:\t%s”,std[1].name);

printf(“\nroll number:\t%d”,std[1].roll);

printf(“\nmarks obtained in 10 subjects are:\n”);

for(j=1;j<=10;j++)

printf(“\nin %dth subject marks obtained=%d”,j,std[1].marks[j]);

printf(“\n\naverage marks:\t%f”,std[1].ave);

getch();

}

How to access the member variables of the structure? (Prev Lesson)
(Next Lesson) Structures within structures
Back to C-Programming

No Comments

Post a Reply

Teacher
Bhim Gautam
Role : Lecturer
Read More
error: Content is protected !!