Home | | Computer Programming | Structures - C Programming

Chapter: Computer Programming

Structures - C Programming

Structure is a collection of variables under the single name, which can be of different data type. In simple words, Structure is a convenient way of grouping several pieces of related information together.

 

Array

 

Single name that contains a collection of data­ items of same data type.

 

Individual entries in a array are called elements.

No Keyword

Members of an array are stored in sequence of memory locations.

 

Structure

Single name that contains a collection of data­items of different data types.

 

Individual entries in a structure are called members.

 

Keyword: struct

 

 

Members of a structure are not stored in sequence of memory locations.

 

 

STRUCTURES

 

Structure is a collection of variables under the single name, which can be of different data type. In simple words, Structure is a convenient way of grouping several pieces of related information together.

 

A structure is a derived data type, The scope of the name of a structure member is limited to the structure itself and also to any variable declared to be of the structure's type.

 

Variables which are declared inside the structure are called “members of structure”.

 

Syntax: In general terms, the composition of a structure may be defined as:

 

struct <tag>

 

{

 

member 1;

 

member 2;

 

­­­­­­­­­­­­

 

­­­­­­­­­­­­­

member m;

 

 

 

where, struct is a keyword, <tag> is a structure name.

 

For example:

 

struct student

 

{

 

char name [80]; int roll_no; float marks;

 

 

Now we need an interface to access the members declared inside the structure, it is called structure variable. we can declare the structure variable in two ways:

 

i) within the structure definition itself.

 

ii) within the main function.

 

i]within the structure definition itself.

 

struct tag

 

{

 

member 1; member 2;

­  ——

 

­  —­

 

­  member m;

 

} variable 1, variable 2 ­­­­­­­­­ variable n; //Structure variables

 

Eg:

 

struct student

 

{

 

char name [80]; int roll_no;

float marks;

 

}s1;

 

ii]within the main function.

 

struct tag

 

{

 

member 1;

 

member 2;

 

­  ——

 

­  —­

 

­  member m; };

void main()

 

{

 

struct tag var1, var2,...,var_n; //structure variable

 

}

 

Eg:

 

struct student

 

{

 

char name [80];

 

int roll_no;

 

float marks;

 

 

void main()

 

{

 

struct student s1, s2; //declaration of structure variable.

 

 

Note: Structure variable can be any of three types: normal variable, array_variable, pointer_variable.

 

Initialization of Structure members:

 

A structure variable, like an array can be initialized in two ways:

 

I. struct student

 

{

 

char name [80]; int roll_no; float marks ;

 

} s1={“Saravana”,34,469};

 

II. struct student

 

{

 

char name [80];

 

int roll_no;

 

float marks ;

 

} s1;

 

struct student s1= {“Saravana”,34,469};

 

[OR]

 

s1.name={“Saravana”};

 

s1.roll_no=34; s1.marks=500;

 

It is also possible to define an array of structure, that is an array in which each element is a structure. The procedure is shown in the following example: struct student

 

{

 

char name [80];

int roll_no ;

 float marks ;

} st [100];

 

In this declaration st is a 100­ element array of structures. It means each element of st represents an individual student record.

 

Accessing the Structure Members

 

The members of a structure are usually processed individually, as separate entities.

 

Therefore, we must be able to access the individual structure members. A structure member can

 

be accessed by writing:

 

structure_variable.member_name //[normal variable]

 

Eg:

 

struct student

 

{

 

char name [80]; int roll_no ; float marks ; }st;

 

e.g. if we want to get the detail of a member of a structure then we can write as scanf(“%s”,st.name); or scanf(“%d”, &st.roll_no) and so on.

 

if we want to print the detail of a member of a structure then we can write as

 

printf(“%s”,st.name); or printf(“%d”, st.roll_no) and so on.

 

The use of the period operator can be extended to arrays of structure by writing:

 

array [expression].member_name //[array variable]

 

Eg:

 

struct student

 

{

 

char name [80];

 

int roll_no ;

 

float marks ; }st[10];

 

e.g. if we want to get the detail of a member of a structure then we can write as scanf(“%s”,st[i].name); or scanf(“%d”, &st[i].roll_no) and so on.

 

if we want to print the detail of a member of a structure then we can write as

 

printf(“%s”,st[i].name); or printf(“%d”, st[i].roll_no) and so on.

 

The use of the period operator can be extended to pointer of structure by writing:

 

array [expression]­>member_name //[array variable]

 

Eg:

 

struct student

 

{

 

char name [80]; int roll_no ; float marks ; }*st;

 

void main()

 

{

 

Struct student s; st=&s;

 

e.g. if we want to get the detail of a member of a structure then we can write as scanf(“%s”,st­>name); or scanf(“%d”, &st­>roll_no) and so on.

 

if we want to print the detail of a member of a structure then we can write as

 

printf(“%s”,st­>name); or printf(“%d”, st­>roll_no) and so on.

 

 

 

It is also possible to pass entire structure to and from functions though the way this is

 

done varies from one version of 'C' to another.

 

PROGRAM’S USING STRUCTURE

 

Example 1://To print the student details// normal structure variable

 

#include<stdio.h> struct student

 

{

 

char name[30]; int reg_no[15]; char branch[30]; };

 

void main()

 

{

 

struct student s1;

 

printf(“\n Enter the student name::”); scanf(“%s”,s1.name);

 

printf(“\n Enter the student register number::”); scanf(“%s”,&s1.reg_no);

 

printf(“\n Enter the student branch::”); scanf(“%s”,s1.branch);

 

printf(“\n Student Name::”,s1.name); printf(“\n Student Branch::”,s1.branch); printf(“\n Student reg_no::”,s1.reg_no);

 

getch();

 

}

 

Example 2://To print the student details// pointer structure variable

 

#include <stdio.h>

 

struct name{

 

int a;

 

float b;

 

 

int main()

 

{

 

struct name *ptr,p;

 

ptr=&p; /* Referencing pointer to memory address of p */

 

printf("Enter integer: "); scanf("%d",&(*ptr).a); printf("Enter number: "); scanf("%f",&(*ptr).b); printf("Displaying: "); printf("%d%f",(*ptr).a,(*ptr).b); return 0;

 

}

 

Example 3://To print the Mark sheet for a student//

 

#include<stdio.h> struct student

 

{

 

char name[30]; int reg_no[15]; char branch[30]; int m1,m2,m3,total; float avg;

 

 

void main()

 

{

 

int total;

 

float avg;

 

struct student s1;

 

printf(“\n Enter the student name::”);

 

scanf(“%s”,s1.name);

 

printf(“\n Enter the student register number::”);

 

scanf(“%s”,&s1.reg_no);

 

printf(“\n Enter the student branch::”);

 

scanf(“%s”,s1.branch);

 

printf(“\n Enter the 3 subjects Marks:”);

 

scanf(“%d%d%d”, &s1.m1,&s1.m2,&s1.m3);

 

s1.total=s1.m1+s1.m2+s1.m3;

 

printf(“\n Ur Total mark is.%d”, s1.total);

 

s1.avg=a1.total/3;

 

printf(“\n Ur Average mark is.%f”, s1.avg);

 

getch();

 

}

 

Example_4: /* To Print Students Mark Sheet's using Structures*/

 

#include"stdio.h"

 

#include"conio.h"

 

struct student

 

{

 

char name[25],grade;

 

int reg_no[15];

 

int s1,s2,s3,s4,s5,total;

 

float avg;

}sa[20];

 

 

void main()

 

{

 

int i,n,total; float avg;

 

printf("\n Enter the count of Students need marksheet::"); scanf("%d",&n);

 

printf("\n Enter the name of students:,reg_no, 5 sub marks::"); for(i=0;i<n;i++)

 

{

 

printf("\n Enter the name of students,reg_no, 5 sub marks::%d",i+1); scanf("%s%d%d%d%d%d%d", &sa[i].name, &sa[i].reg_no, &sa[i].s1, &sa[i].s2, &sa[i].s3, &sa[i].s4, &sa[i].s5);

 

sa[i].total=sa[i].s1+sa[i].s2+sa[i].s3+sa[i].s4+sa[i].s5;

 

sa[i].avg=sa[i].total/5;

 

if((sa[i].s1<50)||(sa[i].s2<50)||(sa[i].s3<50)||(sa[i].s4<50)||(sa[i].s5<50))

 

{

 

sa[i].grade='U';

 

printf("Ur grade is %c", sa[i].grade);

 

}

 

else

 

{

 

if(sa[i].avg==100)

 

{

 

sa[i].grade='S';

 

printf("Ur Grade is %c", sa[i].grade);

 

}

 

if((sa[i].avg>90)&&(sa[i].avg<=99))

 

{

sa[i].grade='A';

 

printf("Ur Grade is %c", sa[i].grade);

 

}

 

 

 

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Computer Programming : Structures - C Programming |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.