Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

STRUCTURES NOTES

Structures
• A structure is a collection of elements of different data types.
• A structure is also called as user defined data type.
• All the elements of a structure are stored at contiguous memory locations.
• A variable of structure type can store multiple data items of different data types under
one name.
• For example, the data of employee in a company that is name, employee ID, salary,
address, phone number is stored in structure data type.
Syntax:
struct <tag_name>
{
data_type1 var1,var2,var3,...;
data_type2 var1,var2,var3,...;
……..
data_typeN var1,var2,var3,....;
};

Example:
struct student
{
int rollno;
char name[10];
float marks;
};
 A structure can be defined by using a keyword “struct”.
 The tag-name specifies name of the structure.
 Defining a structure without structure variable is known as structure template and its size
is always 1 byte.
Structure Variable Declaration
Syntax:
struct <tag_name>
{
datatype1 var1,var2,..;
datatype2 var1,var2,..;
..........
datatypeN var1,var2,..;
};
struct <tag_name> structure_variable_name;
Example - 1:
struct student
{
int rollno;
char name[10];
float marks;
};
struct student s1;

Example - 2: Along with structure


struct student
{
int rollno;
char name[10];
float marks;
}s1;

Structure Variable Initialization


Syntax:
struct <tag_name>
{
datatype1 var1,var2,..;
datatype2 var1,var2,..;
..........
datatype3 var1,var2,..;
};
struct <tag_name> structure_variable_name={val1,valu2,...};

Example-1:
struct student
{
int rollno;
char name[10];
float marks;
};
struct student s1={123,”Ravi”,75.25};

Example-2: Along with structure


struct student
{
int rollno;
char name[10];
float marks;
}s1={123,”Ravi”,75.25};
Accessing Structure Members
• The structure members cannot be directly accessed in the expression.
• They are accessed by using the name of structure variable followed by a dot and then the
name of member variable.
Syntax: structure_variable.stucture_member;
• The method used to access the structure variables are s1.rollno, s1.name, s1.marks.
Structure Memory Allocation

Program-1: Defining a structure outside the main() function.


#include <stdio.h>
struct student
{
char name[30];
int rollno;
float marks;
};
void main()
{
struct student s1={"abc",123,97.75};
printf("Name: %s\n", s1.name);
printf("Roll no: %d\n", s1.rollno);
printf("Marks: %f\n", s1.marks);
}
Program-2: Defining a structure inside the main() function.
#include <stdio.h>
void main()
{
struct student
{
char name[30];
int rollno;
float marks;
};
struct student s1={"abc",123,97.75};
printf("Name: %s\n", s1.name);
printf("Roll no: %d\n", s1.rollno);
printf("Marks: %f\n", s1.marks);
}

Program-3: Initializing structure members individually.


#include<stdio.h>
struct student
{
char *name;
int rollno;
float marks;
};
void main()
{
struct student s1;
s1.name="Abc";
s1.rollno=123;
s1.marks=75.25;
printf("Student details are: \n");
printf("Name: %s \n",s1.name);
printf("Roll Number: %d \n",s1.rollno);
printf("Marks: %.2f \n",s1.marks);
}

Program-4: Copying one structure variable into another.


#include <stdio.h>
#include<string.h>
struct student
{
char name[10];
int rollno;
float marks;
};
void main()
{
struct student s1={"abc",123,85.25};
struct student s2,s3;

// method-1
s2=s1;
printf("student2 details are: \n");
printf("Name: %s\n", s2.name);
printf("Roll Number: %d\n", s2.rollno);
printf("Marks: %f\n", s2.marks);

// method-2
strcpy(s3.name,s1.name);
s3.rollno=s1.rollno;
s3.marks=s1.marks;
printf("student3 details are: \n");
printf("Name: %s\n", s3.name);
printf("Roll Number: %d\n", s3.rollno);
printf("Marks: %f\n", s3.marks);
}
Program-5: Reading students data from keyboard using structure.
#include <stdio.h>
struct student
{
char name[10];
int rollno;
float marks;
};
void main()
{
struct student s1;
// Input
printf("Enter student details: \n");
printf("Enter name: ");
scanf("%s",&s1.name);
printf("Enter roll number: ");
scanf("%d",&s1.rollno);
printf("Enter marks: ");
scanf("%f",&s1.marks);

// Output
printf("Given student details are: \n");
printf("Name: %s\n", s1.name);
printf("Roll Number: %d\n", s1.rollno);
printf("Marks: %f\n", s1.marks);
}

Array of Structures
• The most common use of structure in C programming is an array of structures.
• To declare an array of structure, first the structure must be defined and then an array
variable of that type should be defined.
• Syntax: struct <atg_name> <array_name>[size];
• Example: struct Student s[10]; //10 elements in an array of structures of
type „Student‟
Example Program:
// Program to implement array of structures.
#include<stdio.h>
void main()
{
struct Student
{
char name[10];
int rollno;
float marks;
};
int n,i;
printf("How many students details you want: ");
scanf("%d",&n);
struct Student s[n]; // Arry of structures
for(i=0;i<n;i++)
{
printf("Enter student-%d details: ",i+1);
scanf("%s%d%f",s[i].name,&s[i].rollno,&s[i].marks);
}
printf("All students details are: \n");

for(i=0;i<n;i++)
{
printf("Name:%s ",s[i].name);
printf("Roll Number:%d ",s[i].rollno);
printf("Marks:%.2f \n",s[i].marks);
}
}

Nested Structures
Defining a structure within another structure is known as nested structure. Nested
structures are useful for organizing complex data and can help improve a program's readability
and maintainability.
Syntax:
struct <tag_name>
{
data_type var1,var2,...;
struct <tag_name>
{
data_type var1,var2,..;
……..
} structure_variable_name;
} structure_variable_name;
Example:
struct student
{
char name[20];
int rollno;
float marks;
struct dob
{
int date;
int month;
int year;
}d;
}s;
Accessing Nested Structures
The data member of structure within structure is accessed by using two period (.) symbol.
The syntax to access the structure within structure is
For Example:-
s.d.day;
s.d.month;
s.d.year;
Example Program:
// Program to implement nested structures.
#include <stdio.h>
struct Student
{
char *name;
int rollno;
struct dob
{
int d;
int m;
int y;
}db;
}s1;
void main( )
{
s1.name="Ravi";
s1.rollno=123;
s1.db.d=24;
s1.db.m=04;
s1.db.y=1989;
printf( "Student Name: %s\n",s1.name);
printf( "Student Roll number: %d\n",s1.rollno);
printf( "Student date of birth: %d/%d/%d\n",s1.db.d,s1.db.m,s1.db.y);
}
Structures and Functions
Structure information can be passed to a function in three ways.
1. Passing structure members to function
2. Passing entire structure to function
3. Passing structure to a function by using pointers
Program-1: Passing structure members as parameters to a function
#include <stdio.h>
void function(char name[], int r, float p);
void main()
{
struct student
{
char name[30];
int rollno;
float marks;
};
struct student s1={"Kumar",123,97.75};
function(s1.name,s1.rollno,s1. marks);
}
void function(char n[], int r, float p)
{
printf("Name: %s \n",n);
printf("Roll Number: %d \n",r);
printf("Percentage: %f \n",p);
}

Program-2: Passing entire structure as parameter to a function.


#include <stdio.h>
struct student
{
char name[30];
int rollno;
float marks;
};
void function(struct student stu);
void main()
{
struct student s1={"Kumar",123,97.75};
function(s1);
}
void function(struct student stu)
{
printf("Name: %s\n", stu.name);
printf("Roll no: %d\n", stu.rollno);
printf("Percentage:%f\n", stu.marks);
}
Program-3: Passing structure variable address as parameter to a function.
#include <stdio.h>
struct student
{
char name[30];
int rollno;
float marks;
};
void function(struct student *stu);
void main()
{

struct student s1={"abc",123,97.75};


function(&s1);
}
void function(struct student *stu)
{
printf("Name: %s\n", stu->name);
printf("Roll no: %d\n", stu->rollno);
printf("Percentage:%f\n", stu->marks);
}

Bit Fields
• Bit fields provides exact amount of bits required by the variable.
• Bit fields uses the memory very efficiently.
• The bits must be specified by non-negative(unsigned) integer type followed by a colon(:).
• Bit fields can also used as member in unions.
• Syntax: struct <tag_name>
{
datatype [member_name] : width ;
datatype [member_name] : width ;
};
Example Program:
#include <stdio.h>
void main()
{
struct sample
{
unsigned int b1;
unsigned int b2;
}s1;
struct sample2
{
unsigned int b1:1;
unsigned int b2:1;
}s2;
printf("Size of structure sample : %d ", sizeof(s1));
printf("\nSize of structure sample2 : %d ", sizeof(s2));
}

Enumeration
• Enumeration is an user data type and it can be defined using the keyword “enum”.
Syntax: enum <tag_name>
{
value1, value2, . . . , valueN
};
• By default, value1 will be equal to 0, value2 will be 1 and so on but, the programmer can
change the default value as per requirement.
Example program-1:
#include <stdio.h>
enum Day
{
Sun,Mon,Tue,Wed,Thr,Fri,Sat
};
void main()
{
enum Day today;
today=Wed;
printf("%d",today);
}

Example program-2:
#include <stdio.h>
enum Day
{
Sun,Mon,Tue,Wed,Thr,Fri,Sat
};
void main()
{
int i;
for(i=Sun;i<=Sat;i++)
{
printf("%d ",i);
}
}
Typedef
• Typedef is a keyword in the C language, it is used to define own identifiers that can be
used in place of type specifiers such as int, float, and double.
• A typedef can be used to simplify the declaration for a structure.
Example: typedef struct
{
char name[10];
int rollno;
}student;
• Now we can use student directly to define variables of student type without using struct
keyword.
Example: student s1;
Example program:
#include<stdio.h>
typedef struct telephone
{
char *name;
int number;
}PHONE;
void main()
{
PHONE p1;
p1.name="xyz";
p1.number=12345;
printf("\nName : %s\n",p1.name);
printf("Telephone number: %d\n\n",p1.number);
}

Self Referential Structures


Self Referential structures are those structures that have one or more pointers which point
to the same type of structure, as their member. These types of structures are widely used in
creating dynamic data structures like linked lists, trees and graphs, etc.
Syntax:
struct <tag-name>
{
datatype var1,var2,..;
datatype var1,var2,..;
struct <tag-name> *pointer;
};
Example:
struct node
{
int data;
struct node *next;
};

Unions
• A union is a user defined data type like structure. The union groups logically related
variables into a single unit.
• The union data type allocates the space equal to space needs to hold the largest data
member of union.
• The union allows different types of variable to share same space in memory. The method
to declare, use and access the union is same as structure.
Syntax:
union <tag_name>
{
data_type1 var1,var2,var3,...;
data_type2 var1,var2,var3,...;
……..
data_typeN var1,var2,var3,....;
};

Example:
union student
{
int rollno;
char name[10];
float marks;
};

Example Program:
#include <stdio.h>
union student
{
char name[30];
int rollno;
float percentage;
};
void main()
{

union student s1={"abc",123,97.75};


printf("Name: %s\n", s1.name);
printf("Roll no: %d\n", s1.rollno);
printf("Marks: %f\n", s1.percentage);
}

You might also like