Image

C Language - Structure - Structure

Structure

It is a collection of dissimilar type of data item. It is used to create by using struct keyword. Any variable declare within structure are called structure member.

A structure is a collection of variables referenced under one name providing a convenient means of keeping related information together. The structure definition creates a format that may be used to declare structure variables in a program later on.

Syntax : struct { Datatype ……var1 : : Datatype…… var n } ;

Example

struct student     
{
Int rollno;    
Char name[20];     
Float per;  
} 

It allocates total 26 byte memory. Creating structure variable

1.First way of creating structure variable

struct student 
{
 int rollno;
char name[20];
float per;
} s1,s2,s3….;

2.Second way of creating structure variable struct student s1,s2,s3……..;

Accessing structure member

We can access structure member through structure variable using dot operator.

S1.rollno = 152;
S2.name = “Ashish”;
S3.per = 70.66;


Program to display the value of structure member
#include<stdio.h>
#include<conio.h>
struct student
 {
  char name[20];
  int rollno;
  float marks;
 };
main()
  {
  clrscr();
   struct student stu1={"mary",25,68}; //first way to insert data
   struct student stu2,stu3;
   strcpy (stu2.name,"prashant");
   stu2.rollno=26;             // second way to insert data
   stu2.marks=98;
   printf("\n Enter name roll no marks for stu3:");   // this is third way
   scanf("%s%d%f",stu3.name,&stu3.rollno,&stu3.marks);
   printf("\n %4s %4d %.2f \n",stu1.name,stu1.rollno,stu1.marks);
   printf("%4s %4d %.2f \n",stu2.name,stu2.rollno,stu2.marks);
   printf("%4s %4d %.2f \n",stu3.name,stu3.rollno,stu3.marks);
   getch();
  }
o/p
Enter name rollno ,marks for stu3: pp
32333
56
Marry  25  68.00
Prashant  26  98.00
pp   78  6.00
Program to understand pointer to structure
#include<stdio.h>
#include<conio.h>
struct student
{
 char name[10];
 int rollno;
 float marks;
};
main()
{
 clrscr();
 struct student stu={"sonu",34,45};
 struct student *ptr = &stu;
 printf("\n name-%s\t",ptr ->name);
 printf("\n Rollno-%d\t",ptr ->rollno);
 printf("\n marks -%.2f",ptr ->marks);
 getch();
}
o/p
name – sonu
rollno – 34
marks – 45.00
Passing Structures to Functions

There are three methods by which the values of a structure can be transferred from one function to another

The first method is to pass each member of the structure as an actual argument of the function call. The actual arguments are then treated independently like ordinary variables.

The second method involves passing of a copy of the entire structure to the called function. Since the function is working on a copy of the entire structure to the called function, changes are not reflected in the original structure (in the calling function). It is, therefore, necessary for the function to return the entire structure back to the calling function.

The third approach employs a concept called pointers to pass the structure as an argument. In this case, the address location of the structure is passed to the called function. The function can access indirectly the entire structure and work on it.

EX : Program passing entire structure as function parameter
struct emp
{
char empname [25];
char company [25];
int empno;
}
main( )
{
static struct emp emp1 = {"Prashant", “ABC", 101};
display (emp1);
}
display (e)
struct emp e;
{
printf ("%s\n%s\n%d", emp.empname, emp.company,
emp.empno);
}
Output:
Prashant
ABC
101