STRUCTURES IN C PROGRAMMING

STRUCTURES IN C
STRUCTURES IN C PROGRAMMING


What is a Structure

Structure is a user defined datatype which contains collection of elements of both similar and dissimilar data types, grouped together under a single name.

For example 

1.If you want to store the information about a student about his                 name,age,gender,class and section.

2.These Information contains different datatypes,since all this information is related a single person [a single name] a better   approach would be a collection of all these information under  single name.

3.Therefore for this purpose Function is used.

DECLARATION OF A STRUCTURE
The keyword struct is used to delare structure followe by structure tag.
The Syntax is shown below :
 struct structure_name
 {
   data_type variable_name1;
   data_type variable_name2;
   data_type variable_name3;
 };

Declaration of the above student example using Structure

struct Student
{
    char name[25];
    int age;
    int class;
    char section;
    char gender; // F for female and M for male
};

DECLARATION OF STRUCTURE VARIABLES

The structure variables can be declared using 2 methods:

1) Declaring Structure variables separately

Example showing how structure variables are declared separately :
 struct struct Student
{
   char name[25];
    int age;
    int class;
    char section;
    char gender; // F for female and M for male
}; struct Student S1, S2; //declaring variables of struct student

2) Declaring Structure variables with structure definition

Example showing how structure variables are declared along with the structure definition

struct Student
{
    char name[25];
    int age;
    int class;
    char section;
    char gender; // F for female and M for male
}S1,S2;

In the Above examples S1 and S2 are variables of structure student.

STRUCTURE VARIABLE INITIALISATION

Initializing a structure means assigning some constant values to the members of structure. The  syntax for initializing the structure member variables is:  

struct structure_name structure_var = {constantl, constant2.....};

Example :

struct student

{  

char name[50];  

int age;  

};  

struct student s1={"ram", 20};  //declaring variables of struct student

ACCESSING MEMBERS OF A STRUCTURE

There are three ways of accessing members of structures  

1. Dot operator ( . )  

2. Indirection operator (* )  

3. Selection operator (-->) 

The Last two are used with pointers we will learn about them in pointers.

1. Dot operator  

The members of a structure can be accessed by using (.) dot operator. Any member of a structure  can be accessed as: Syntax:  

structure_variable_name.member_name;  

Example: sl.name="Ram" ; sl.age=18;  

Reading and writing structure variable: Based on the member type structure members can use  input and output statements for reading writing  

Reading example: scanf("%s", s1.name); or sl.name=gets (); scanf("%d",&s1.age); 

Writing example: printf("%s", s1.name); or puts (sl.name); printf("%d", s1.age);  

Program example:Below is a C program to create a structure student with fields: name, usn,  age, marks and gender. Read details of student and display it.

 #include<stdio.h>  
 struct student  
 {  
 char name [50];  
 char rollno[10];  
 int age;  
 float marks;  
 }s;  
 void main()  
 {  
 printf ("\n Enter the details of Student\n");  
 printf("\n Name: ");  
 scanf("%s", s.name);  
 printf(" Roll Number: ");  
 scanf("%s",s.rollno);  
 printf(" Age: ");  
 scanf("%d",&s.age);  
 printf(" Marks: ");  
 scanf("%f",&s.marks);  
 printf("\n The student details are");  
 printf("\n Name: %s", s.name);  
 printf("\n Roll Number: %s",s.rollno);  
 printf("\n Age: %d",s.age);  
 printf("\n Marks: %f", s.marks);  
 }  
Output : 
Enter the details of Student 

 Name: RAM 
 Roll Number: 05
 Age: 21
 Marks: 85 

 The student details are 
 Name: RAM 
 Roll Number: 05 
 Age: 21
 Marks: 85.000000


Post a Comment

0 Comments