Structure in C

Structure in C

We all know ordinary variable can hold single information, when want to store information  of similar data types we use array, these two method handle most of the situations but sometime we need collect information of dissimilar data types. For example suppose we want to store data about student.

You might want to store student name (a string data type), its roll number (an integer data type) and marks in percentage (a float data type) if data about say 3 such students is to be stored, then we can do this by following two approaches.

1. We can construct individual array, one for storing name of student, and one for storing      roll no and one for storing percentage of marks.

         2.  We use a structure variable.

In first approach program becomes more difficult to handle as the number of information items related to students are go on increasing. For example we need to increase number of array, if we also decide to store student address, date of admission, etc. to solve this problem a second approach is provided in C programming i.e. the structure.

 A structure is a collection of different data type variables under one name. A structure is basically user defined data type, the variables which are present in  structure are called structure element or member of a structure.

Declaring a structure

struct keyword is used to define the structure, and syntax of declaration is:

struct  structure_name

{

    data_type member1;

    data_type member2;

    .

    .

    data_type memeberN;

};

 

Example:

struct student

{

  char sname;

int rollno:

float percentage;

};

This statement defined a new data type called struct student. Each variable of this data type will consist of a character called sname, a int variable called rollno and a float variable called percentage.

Once the new structure defined in that one or more variables you can declare in that type . for example variable s1,s2,s3 can be declared to be of the type struct student as,

struct student s1,s2,s3;

this statement  sets one side space in memory. It provide space to hold all the element in the structure.

 We can also combine declaration of  structure  type with structure variable in single statement .

For example,

 struct student

{

  char sname;

int rollno:

float percentage;

};

struct student s1,s2,s3;

structure variable can be initialized where they are declared and format quite similar to as we initialize array.

 struct student

{

  char sname[10];

int rollno:

float percentage;

};

struct student s1={ “BOB”, 21,60.75};

struct student s2={“SARA”22,75.60};

A structure is variable declaration, so it should be end with semicolon.

When we declare a structure type declaration doesn’t tell to compiler to reserve  any space in memory

Accessing structure elements

 In array we use subscript to access  the array element, in structure we use different arrangement , we use a dot(.) operator.

Before the dot operator the structure variable and after the dot must be a structure element.

 For example

S1.sname

 All the structure elements are stored in contiguous memory location.

 Example of structure: to store student record

 #include <stdio.h>

#include<conio.h>

struct student

  {

  char sname[50];

  int rollno;

  float marks;

  }s1,s2,s3;

int main()

{

clrscr();

printf("\n Enter student name, roll no and percentage of  3  student\n");

scanf("%s %d %f", &s1.sname, &s1.rollno, &s1.marks);

scanf("%s %d %f", &s2.sname, &s2.rollno, &s2.marks);

scanf("%s %d %f", &s3.sname, &s3.rollno, &s3.marks);

printf("\n Details of record you entered");

printf("\n%s %d %f", s1.sname, s1.rollno, s1.marks);

printf("\n%s %d %f", s2.sname, s2.rollno, s2.marks);

printf("\n%s %d %f", s3.sname, s3.rollno, s3.marks);

getch();

return 0;

 }

  In this program structure student is created and it has three members sname( string data type), rollno (int data type)and marks(float data type).

Summary

 Structure mostly used to store dissimilar data together like employee data, books data, student data.

Structure element accessed through a structure variable using dot operator.


Previous
Next Post »