Structures
• Structure is user defined data type, which contain individual elements
that can be different type. Single structure might contain integer
elements, floating-point elements and character elements.
• It is a convenient tool for handling a group of logically related data
items.
• Arrays, pointers and other structures (nested structure) can be
included in structures
• Structures are used represent a record. Examples: (1) Student name,
roll number, and marks, (2) Real part and complex part of a complex
number. The individual elements of a structure are called members.
2
Structure declarations: -
struct tag
{
member 1;
member 2;
.....
member m;
};
• struct is a required keyword; tag is a name that identifies structures
of this type.
• The individual members can be ordinary variables, pointers, arrays,
or other structures. 3
Example:-
A structure definition:
• Defining structure variables:
4
struct account {
int acct_no;
char acct_type;
char name[80];
float balance;
};
5
Processing (or initializing) Structure:-
• The members of a structure are processed individually, as separate entities.
Therefore, we must be able to access the individual structure members.
• A structure member can be accessed as:
variable.member
• where variable refers to-the name of a structure-type variable, member refers to
the name of a member within the structure.
• the period (.) that separates the variable name from the member name.
6
#include <stdio.h>
struct VIT
{
char slot;
int id;
int age;
};
int main()
{
struct VIT s1;
s1.slot = 'A';
s1.id= 1234;
s1.age = 22;
printf("Student slot is: %c", s1.slot);
printf("\nStudent Id is: %d", s1.id);
printf("\nStudent Age is: %d", s1.age);
return 0;
} 7
#include <stdio.h> int main()
struct VIT {
{ struct VIT s1;
char slot; printf("enter yout slot: ");
int id; scanf("%c", &s1.slot );
int age; printf("enter yout id: ");
}; scanf("%d", &s1.id );
printf("enter yout age: ");
scanf("%d", &s1.age );
printf("Student slot is: %c", s1.slot);
printf("\nStudent Id is: %d", s1.id);
printf("\nStudent Age is: %d", s1.age);
return 0;
}
8
Write a program in C language with the structure name “CSBS_student”. The
members of this structure are the student's name, registration number, and the
student's marks. Now create a new variable of the structure “CSBS_student” and
access the structure members. All inputs are user-defined only.
9
#include <stdio.h> int main()
struct CSBS_student {
{ struct CSBS_student s1;
char name[20]; printf("enter your name: ");
int reg_no; scanf("%s", s1.name );
int marks; printf("enter your reg no: ");
}; scanf("%d", &s1.reg_no);
printf("enter your marks: ");
scanf("%d", &s1.marks );
printf("Student slot is: %s", s1.name);
printf("\nStudent Id is: %d", s1.reg_no);
printf("\nStudent Age is: %d", s1.marks);
return 0;
}
10
Define a structure type, struct personal that would contain person name, date of
joining and salary. Write a program to read this information for one person from the
keyboard and print the same on the screen.
11
Write a C Program to Add Two Complex Numbers using structure?
12
Arrays of Structures: -
Why use an array of structures?
• Consider a case, where we need to store the data of 5 students consisting of
student name, student id and student marks.
• We will have to declare 5 different structure variables and store them one by one.
13
#include <stdio.h>
struct Student
{
char name[20];
int id;
int marks;
};
int main()
{
struct Student s1, s2, s3, s4, s5;
printf("enter your name, id and marks of student 1 ");
scanf("%s %d %d", s1.name, &s1.id, &s1.marks );
printf("enter your name, id and marks of student 2 ");
scanf("%s %d %d", s2.name, &s2.id, &s2.marks );
printf("enter your name, id and marks of student 3 ");
scanf("%s %d %d", s3.name, &s3.id, &s3.marks );
14
printf("enter your name, id and marks of student 4 ");
scanf("%s %d %d", s4.name, &s4.id, &s4.marks );
printf("enter your name, id and marks of student 5 ");
scanf("%s %d %d", s5.name, &s5.id, &s5.marks );
printf("Student 1 details are: %s %d %d \n", s1.name, s1.id, s1.marks );
printf("Student 2 details are: %s %d %d \n", s2.name, s2.id, s2.marks );
printf("Student 3 details are: %s %d %d \n", s3.name, s3.id, s3.marks );
printf("Student 4 details are: %s %d %d \n", s4.name, s4.id, s4.marks );
printf("Student 5 details are: %s %d %d \n", s5.name, s5.id, s5.marks );
return 0;
}
However, the complexity of the program will be increased if there are 20 students.
This will always be tough since we will have to declare a variable every time we add a
student. Remembering the name of all the variables is also a very tricky task.
15
To resolve the above problem, we are using the Arrays ofStructures.
• Once a structure has been defined, we can declare an array of structures.
struct student class[50];
• The individual members can be accessed as:
class[i].name
class[5].roll number
16
#include <stdio.h>
struct Student
{
char name[20];
int id;
int marks;
};
int main()
{
struct Student s1[5];
for(int i=0; i<5; i++)
{
printf("enter name, id and marks of students:");
scanf("%s %d %d", s1[i].name, &s1[i].id, &s1[i].marks);
}
for(int i=0; i<5; i++)
printf("\nStudent details are: %s %d %d", s1[i].name, s1[i].id, s1[i].marks);
return 0; } 1
7
Write a C Program to using structure for the following?
Get the student's name, its id and marks from the user. After that display the highestmarks.
18
#include <stdio.h>
struct Student
{
char name[20];
int id;
int marks;
};
int main()
{
struct Student s1[10];
int max=0;
for(int i=0; i<10; i++)
{
printf("enter name, id and marks of students:");
scanf("%s %d %d", s1[i].name, &s1[i].id, &s1[i].marks);
}
19
for(int i=0; i<10; i++)
{
if(max<s1[i].marks)
{
max=s1[i].marks;
}
}
printf(" maximum marks is: %d", max);
return 0;
}
20
A structure can be passed as argument to a function.
#include <stdio.h> int main()
{
struct student struct student s1;
{ printf("Enter student name, id and age:");
char name[50]; scanf("%s %d %d", s1.name, &s1.id, &s1.age);
int id; display(s1);
int age; return 0;
}; }
void display(struct student s2)
void display(struct student s2); {
printf("\nDisplaying student information\n");
printf("Student name: %s \n Student id: %d \n
Student age: %d", s2.name, s2.id, s2.age);
}
2
#include <stdio.h>
struct student
void display1(struct student s2)
{
{
char name[50];
printf("\nDisplaying student information\n");
int id;
printf("Student name: %s \n Student id: %d \n
int age; Student age: %d", s2.name, s2.id, s2.age);
};
}
void display1(struct student s2);
void display2(char a[], int b, int c)
void display2(char a[], int b, int c);
{
printf("\nDisplaying student information\n");
int main()
printf("Student name: %s \n Student id: %d \n
{
Student age: %d", a, b, c);
struct student s1;
}
printf("Enter student name, id and age:");
scanf("%s %d %d", s1.name, &s1.id,
&s1.age);
display1(s1);
display2(s1.name, s1.id, s1.age);
3
return 0; }
Consider a class having 10 students. Every student has a name and roll no. and the
subjects are math, physics, and chemistry.
Write a program in C using structure to read the data from user and determine the
following:
Total marks obtained by each student.
The highest marks in each subject and the Roll No. of the student who
secured it.
The student who obtained the highest total marks.
4
struct student {
char name[30];
int rollno;
int sub[3]; };
void main() {
int i, j, max, count, total, n, a[10], ni;
struct student st[10];
printf("Enter how many students: ");
scanf("%d", &n);
/* for loop to read the names and roll numbers*/
for (i = 0; i < n; i++) {
printf("\nEnter name and roll number for student %d :
", i);
scanf("%s", &st[i].name);
scanf("%d", &st[i].rollno); }
5
/* for loop to read ith student's jth subject*/
for (i = 0; i < n; i++) {
for (j = 0; j <= 2; j++) {
printf("\nEnter marks of student %d for subject %d : ", i, j);
scanf("%d", &st[i].sub[j]); }}
/* (i) for loop to calculate total marks obtained by each student*/
for (i = 0; i < n; i++) {
total = 0;
for (j = 0; j < 3; j++) {
total = total + st[i].sub[j]; }
printf("\nTotal marks obtained by student %s are %dn", st[i].name,total);
a[i] = total;
}
6
/* (ii) for loop to list out the student's roll numbers who have secured the
highestmarks in each subject */
/* roll number who secured the highest marks */
for (j = 0; j < 3; j++) { max = 0;
for (i = 0; i < n; i++) {
if (max < st[i].sub[j]) {
max = st[i].sub[j]; ni = i; }}
printf("\nStudent %s got maximum marks = %d in Subject :
%d",st[ni].name, max, j); }
max = 0;
for (i = 0; i < n; i++) {
if (max < a[i])
{max = a[i];
ni = i; } }
printf("\n%s obtained the total highest marks.", st[ni].name);}
7
Structure Pointer:-
• The beginning address of a structure can be accessed in the same manner as any
other address, through the use of the address (&) operator.
• The way we can have a pointer pointing to an int, or a pointer pointing to a char,
similarly we can have a pointer pointing to a struct. Such pointers are known as
‘structure pointers’.
• If variable represents a structure type variable, then & variable represents the
starting address of that variable. We can then assign the beginning address of a
structure variable to this pointer by writing ptr= &variable;
struct name {
member1;
member2; };
int main() {
struct name *ptr, s1; } 8
2
9
Access structure members using Pointer:-
#include <stdio.h>
struct person
{
int age;
float weight;
};
int main() {
struct person p1, *ptr;
ptr = &p1;
printf("Enter age and weight of person: ");
scanf("%d %f", &ptr->age, &ptr->weight );
printf("Age: %d\n", ptr->age);
printf("weight: %f", ptr->weight);
return 0; }
Note:- ptr>age is equivalent to (*ptr).age 1
0
3
5
What is the output of this C code:-
#include <stdio.h>
struct person {
int age;
float weight;
};
int main() {
struct person p1, p2, *ptr;
ptr = &p1;
printf("Enter age and weight of person: ");
scanf("%d %f", &ptr->age, &ptr->weight );
scanf("%d %f", &p2.age, &p2.weight );
printf("Age: %d\n", ptr->age);
printf("weight: %f", ptr->weight);
printf("Age: %d\n", p2.age);
printf("weight: %f", p2.weight);
return 0; } 4
What is the output of this C code:-
#include <stdio.h>
struct book{
char name[25] ;
char author[25] ;
int cellno ;
};
void main( )
{
struct book b1 = { "C programming", "cccc", 101 } ;
struct book *ptr ;
ptr = &b1 ;
printf ( "\n%s %s %d", b1.name, b1.author, b1.cellno ) ;
printf ( "\n%s %s %d", ptr->name, ptr->author, ptr->cellno ) ;
}
5
Access array of structure members using Pointer:-
#include <stdio.h>
struct person ptr = p1;
{ for (int i = 0; i < 3; i++)
int age; {
float weight; printf("Age: %d\n", ptr->age);
}; printf("weight: %f", ptr->weight);
int main() ptr++;
{ }
struct person p1[3], *ptr; return 0;
ptr = p1; }
printf("Enter age and weight of person: ");
for (int i = 0; i < 3; i++)
{
scanf("%d %f", &ptr->age, &ptr->weight );
ptr++;
}
6
Question:-
Consider a class having 60 students. Every student has a name, department name, its
roll no., and mobile no. Write a program in C using pointers of structure to read the data
from user and then display the student details as the output.
7
typedef:-
• The typedef is a keyword that is used to provide existing data types with a new name. The
C typedef keyword is used to redefine the name of already existing data types.
• The typedef feature allows users to define new data-types that are equivalent to existing
data types.
• When names of datatypes become difficult to use in programs, typedef is used with user-
defined datatypes, which behave similarly to defining an alias for commands.
• Once a user-defined data type has been established, then new variables, arrays,
structures, etc. can be declared in terms of this new data type.
• Syntax:-
typedef type new- type;
Where, type refers to an existing data type, and new- type refers to the new user-defined
data type 2
#include <stdio.h> #include <stdio.h>
typedef int age; typedef long long ll;
int main() int main()
{ {
age male = 30; ll a = 10, b = 20;
age female = 20; ll c = a+b;
printf("%d \n %d", male, female); printf("%ld", c);
return 0; return 0;
} }
3
#include <stdio.h>
typedef struct student
{
char name[20];
int age;
}stud;
int main()
{
stud s1, s2;
printf("Enter the name and age of the student 1:");
scanf("%s %d",&s1.name, &s1.age);
printf("\nEnter the name and age of the student 2:");
scanf("%s %d",&s2.name, &s2.age);
printf("Name and age of the student 1 is : %s %d", s1.name, s1.age);
printf("\n Name and age of the student 2 is : %s %d", s2.name, s2.age);
return 0;
}
4
#include <stdio.h> #include <stdio.h>
typedef int* ptr; typedef int Arr[4];
int main() int main()
{ {
int a=10; Arr a = {10, 20, 30, 40};
ptr b, c; for (int i = 0; i < 4; i++)
b=&a; {
c=&a; printf("%d ", a[i]);
*b = 20; }
printf("Value is %d \n", a); return 0;
printf("Value is %d %d", *b, *c); }
return 0;
5
union: -
• A union is a special data type available in C that allows to store different data types in the same
memory location.
• You can define a union with many members, but only one member can contain a value at any
given time. The memory occupied by a union will be large enough to hold the largest member
of the union.
• Unions provide an efficient way of using the same memory location for multiple-purpose.
struct student{ union student{
int a; int a;
float b float b
char c; char c;
} a1; } a1;
Memory for a1 = (2 +4+1) bytes Memory for a1 = 4 bytes
6
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
struct Data { union Data {
int i; int i;
float f; float f;
char str[20]; char str[20];
}; };
int main( ) int main( )
{ {
struct Data data; union Data data;
printf( "Memory size occupied by printf( "Memory size occupied by data :
data : %d\n", sizeof(data)); %d\n", sizeof(data));
return 0; return 0;
} }
7
Memory size occupied by data : 28 Memory size occupied by data : 20
8
Difference between structure and Union:-
Structure Union
The keyword struct is used to define a The keyword union is used to define a
structure union.
When a variable is associated with a When a variable is associated with a union,
structure, the compiler allocates the the compiler allocates the memory by
memory for each member. The size of considering the size of the largest memory.
structure is greater than or equal to thesum So, size of union is equal to the size of largest
of sizes of its members. The smaller member.
members may end with unused slack bytes.
Each member within a structure is assigned Memory allocated is shared by individual
unique storage area of location. members of union.
9
The address of each member will be in The address is same for all the members of a
ascending order This indicates that memory union. This indicates that every member
for each member will start at different begins at the same offset value.
offset values.
1
0