SlideShare a Scribd company logo
PRESEG
K
1
2
Submitted By:
Gurvinder Kaur
L-2019-A-22-BTFT
COMPUTER PROGRAMMING AND
DATA STRUCTURES
Submitted To:
Mr. Ashok Kumar
STRUCTURES
3
In C programming, a struct
(or structure) is a collection
of variables (can be of
different types) under a
single name.
4
5
How to define
structures?
To define a struct,
the struct keyword
is used.
Structure always ends
with a semicolon(;)
struct employees
{
char name[20];
int age;
float salary;
};
Here is an example:
struct structure_Name
{
dataType member1;
dataType member2;
... };
Syntax of struct
Here, a derived type structure employees is defined.
6
Create structure
variables
When a struct type is
declared, no storage or
memory is allocated. To
allocate memory of a given
structure type and work
with it, we create variables
struct employees
{
char name[20];
int age;
float salary;
};
int main()
{
struct employees employee1, employee2, e[20];
return 0;
}Two variables employee1,
employee2, and an array variable e
having 20 elements of type struct
employees are created.
Alternative method
struct tag
{
member 1;
member 2;
....
member m;
} var_1, var_2,...var_n;
Declares three
variables of type
struct tag
7
Access members
of a structure
There are two types of
operators used for accessing
members of a structure.
. - Member operator
-> - Structure pointer operator
EXAMPLE:-
To access the salary of employee2
employee2.salary
 In order to assign a value to any
structure member, the member name
must be linked with
the structure variable using a
dot . operator also
called period or member
access operator.
#include <stdio.h>
#include <conio.h>
struct employees
{
char name[20];
int age;
float salary;
};
int main( )
{
/* Declare employee1 of type employees */
struct employees employee1 ={“Sofia”,27, 35000};
/* Declare employee2 of type employees */
struct employees employee2={“Blossom”,30, 30000};
/* print employee1 info */
printf( “employee1 name: %sn", employee1.name);
printf( “employee1 age: %dn", employee1.age);
printf( “employee1 salary: %fn", employee1.salary);
/* print employee2 info */
printf( “employee2 name: %sn", employee2.name);
printf( " employee2 age : %dn", employee2.age);
printf( “employee2 salary: %fn", employee2.salary);
return 0;
}
PROGRAM TO ACCESS AND PRINT MEMBERS OF A STRUCTURE
employee1 name: Sofia
employee1 age: 27
employee1 salary: 35000.0
employee2 name: Blossom
employee2 age: 30
employee2 salary: 30000.0
OUTPUT
8
9
Structures as
Function
Arguments
We can pass a structure
as a function argument
in the same way as we
pass any other variable
or pointer.
struct employees
{
char name[20];
int age;
float salary;
};
int manager()
{
struct employees manager;
………
}
int main()
{
struct employees employee1;
struct employees employee2;
………..
}
Manager information is
maintained in a separate
function.
So function manager() is
declared. We have declared a
variable manager in it which is
local to the function
manager()
10
#include<stdio.h>
#include<conio.h>
struct employees
{
char name;
int age;
int salary;
};
int manager()
{
struct employees manager;
manager.age=28;
if(manager.age>30)
manager.salary=65000;
else
manager.salary=55000;
return manager.salary;
}
int main()
{ struct employees employee1;
struct employees employee2;
printf(“Enter the salary of employee1:”);
scanf(“%d”, &employee1.salary);
printf(“Enter the salary of employee2:”);
scanf(“%d”, &employee2.salary);
printf(“ Employee 1 salary is: %dn”,employee1.salary);
printf(“ Employee 2 salary is: %dn”,employee2.salary);
printf(“ Manager’s salary is: %dn”,manager());
return 0;
}
OUTPUT
Enter the salary of employee1: 35000
Enter the salary of employee2: 20000
Employee 1 salary is: 35000
Employee 2 salary is: 20000
Manager’s salary is: 55000
PROGRAM SHOWING STRUCTURE AS FUNCTION ARGUMENTS
Array of Structures in
C
An array of structures in C can be
defined as the collection of multiple
structures variables where each variable
contains information about different
entities.
The array of structures in C are used to store
information about multiple entities of different
data types. The array of structures is also
known as the collection of structures.
Once a structure has been
defined, we can declare an
array of structures
for example;
struct student class[50];
The individual members
can be accessed as
class[i].name
class[5].roll_number
Example of an array of structures that stores information of 3
students and prints it.
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
int main()
{
int i;
struct student st[3];
printf("Enter Records of 3 students");
for(i=0;i<3;i++)
{
printf("nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("nEnter Name:");
scanf("%s",&st[i].name);
}
printf("nStudent Information List:");
for(i=0;i<3;i++){
printf("nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
OUTPUT
Enter Records of 3 students
Enter Rollno: 1
Enter Name: Jamie
Enter Rollno: 2
Enter Name: Ratan
Enter Rollno: 3
Enter Name: John
Student Information List:
Rollno:1, Name: Jamie
Rollno:2, Name: Ratan
Rollno:3, Name: John
Pointers to
Structures
13
struct abc
{
int x;
int y;
};
int main()
{
struct abc a={0,1};
struct abc *ptr=&a;
printf(“%d %d”, ptr->x, ptr->y);
return 0;
}
ptr is a pointer to
some variable of
type struct abc
We can define pointers to structures in
the same way as we define pointer to
any other variable
We can store the address of a structure
variable (a) in the defined pointer
variable(*ptr)
To find the address of a structure
variable, place the '&'; operator before
the structure's name
To access the members of a structure
using a pointer to that structure, we use
the → operator ;
0,1
OUTPUT
14
Nested
Structure in C
C provides us the feature of
nesting one structure within
another structure by using
which, complex data types are
created.
Nested structures means, that one
structure has another stucture as
member variable.
struct student
{
char[30] name;
int age;
/* here Address is a structure */
struct Address
{
char[50] locality;
char[50] city;
int pincode;
} addr;
WATCHING
THANKS
FOR

More Related Content

PPTX
PPTX
Structures
DOC
Unit 5 (1)
PPSX
C programming structure & pointer
PPT
pointer, structure ,union and intro to file handling
PDF
CS50 Lecture4
Structures
Unit 5 (1)
C programming structure & pointer
pointer, structure ,union and intro to file handling
CS50 Lecture4

What's hot (20)

PPTX
Structure & union
PDF
C語言基本資料型別與變數
PPTX
Function basics
PPT
Structure and union
PPTX
Structures and Unions
PDF
Roadmap to Object Oriented Programming
PDF
PDF
L10
PDF
Listing for TryLambdaExpressions
PDF
201801 CSE240 Lecture 09
PPTX
Structure & union
PPTX
Presentation for structure in c
PPTX
Structure & Union in C++
PPTX
งานนำเสนอ1
PDF
VIT351 Software Development VI Unit4
PDF
OO-like C Programming: Struct Inheritance and Virtual Function
PPTX
Lab 13
PPT
Structures
Structure & union
C語言基本資料型別與變數
Function basics
Structure and union
Structures and Unions
Roadmap to Object Oriented Programming
L10
Listing for TryLambdaExpressions
201801 CSE240 Lecture 09
Structure & union
Presentation for structure in c
Structure & Union in C++
งานนำเสนอ1
VIT351 Software Development VI Unit4
OO-like C Programming: Struct Inheritance and Virtual Function
Lab 13
Structures
Ad

Similar to STRUCTURES IN C PROGRAMMING (20)

PPTX
detail structure presentation of problem solving
PPTX
Programming for problem solving-II(UNIT-2).pptx
PDF
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
PDF
03 structures
PPTX
data structure and c programing concepts
PPTX
Module 5-Structure and Union
PDF
Principals of Programming in CModule -5.pdf
PPT
structure.ppt
PDF
C structure and union
PPTX
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
PPT
C Language_PPS_3110003_unit 8ClassPPT.ppt
DOCX
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
PPT
Presentation on structure,functions and classes
PDF
structure1.pdf
PDF
Lk module4 structures
PDF
unit 5.pdf structure pdf is here you can do this
PDF
Chapter15 structure
PPTX
Chapter 2 part II array and structure.pptx
PDF
Pointers and Structures
PPT
Unit4 C
detail structure presentation of problem solving
Programming for problem solving-II(UNIT-2).pptx
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
03 structures
data structure and c programing concepts
Module 5-Structure and Union
Principals of Programming in CModule -5.pdf
structure.ppt
C structure and union
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
C Language_PPS_3110003_unit 8ClassPPT.ppt
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
Presentation on structure,functions and classes
structure1.pdf
Lk module4 structures
unit 5.pdf structure pdf is here you can do this
Chapter15 structure
Chapter 2 part II array and structure.pptx
Pointers and Structures
Unit4 C
Ad

More from Gurwinderkaur45 (7)

PPTX
ANCIENT METHODS OF FOOD PRESERVATION
PDF
ANTIOXIDANTS POWERPOINT PRESENTATION
PPTX
Animatronics POWERPOINT PRESENTATION
PPTX
Lifi powerpoint presentation
PPTX
plant hormones powerpoint presentation
PDF
IMMUNITY AND THE IMMUNE SYSTEM
PPTX
Pros and cons of Science and technology
ANCIENT METHODS OF FOOD PRESERVATION
ANTIOXIDANTS POWERPOINT PRESENTATION
Animatronics POWERPOINT PRESENTATION
Lifi powerpoint presentation
plant hormones powerpoint presentation
IMMUNITY AND THE IMMUNE SYSTEM
Pros and cons of Science and technology

Recently uploaded (20)

PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Welding lecture in detail for understanding
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPT
Project quality management in manufacturing
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Construction Project Organization Group 2.pptx
PDF
Well-logging-methods_new................
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Welding lecture in detail for understanding
CYBER-CRIMES AND SECURITY A guide to understanding
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Operating System & Kernel Study Guide-1 - converted.pdf
Project quality management in manufacturing
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
OOP with Java - Java Introduction (Basics)
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
CH1 Production IntroductoryConcepts.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Construction Project Organization Group 2.pptx
Well-logging-methods_new................

STRUCTURES IN C PROGRAMMING

  • 2. 2 Submitted By: Gurvinder Kaur L-2019-A-22-BTFT COMPUTER PROGRAMMING AND DATA STRUCTURES Submitted To: Mr. Ashok Kumar
  • 4. In C programming, a struct (or structure) is a collection of variables (can be of different types) under a single name. 4
  • 5. 5 How to define structures? To define a struct, the struct keyword is used. Structure always ends with a semicolon(;) struct employees { char name[20]; int age; float salary; }; Here is an example: struct structure_Name { dataType member1; dataType member2; ... }; Syntax of struct Here, a derived type structure employees is defined.
  • 6. 6 Create structure variables When a struct type is declared, no storage or memory is allocated. To allocate memory of a given structure type and work with it, we create variables struct employees { char name[20]; int age; float salary; }; int main() { struct employees employee1, employee2, e[20]; return 0; }Two variables employee1, employee2, and an array variable e having 20 elements of type struct employees are created. Alternative method struct tag { member 1; member 2; .... member m; } var_1, var_2,...var_n; Declares three variables of type struct tag
  • 7. 7 Access members of a structure There are two types of operators used for accessing members of a structure. . - Member operator -> - Structure pointer operator EXAMPLE:- To access the salary of employee2 employee2.salary  In order to assign a value to any structure member, the member name must be linked with the structure variable using a dot . operator also called period or member access operator.
  • 8. #include <stdio.h> #include <conio.h> struct employees { char name[20]; int age; float salary; }; int main( ) { /* Declare employee1 of type employees */ struct employees employee1 ={“Sofia”,27, 35000}; /* Declare employee2 of type employees */ struct employees employee2={“Blossom”,30, 30000}; /* print employee1 info */ printf( “employee1 name: %sn", employee1.name); printf( “employee1 age: %dn", employee1.age); printf( “employee1 salary: %fn", employee1.salary); /* print employee2 info */ printf( “employee2 name: %sn", employee2.name); printf( " employee2 age : %dn", employee2.age); printf( “employee2 salary: %fn", employee2.salary); return 0; } PROGRAM TO ACCESS AND PRINT MEMBERS OF A STRUCTURE employee1 name: Sofia employee1 age: 27 employee1 salary: 35000.0 employee2 name: Blossom employee2 age: 30 employee2 salary: 30000.0 OUTPUT 8
  • 9. 9 Structures as Function Arguments We can pass a structure as a function argument in the same way as we pass any other variable or pointer. struct employees { char name[20]; int age; float salary; }; int manager() { struct employees manager; ……… } int main() { struct employees employee1; struct employees employee2; ……….. } Manager information is maintained in a separate function. So function manager() is declared. We have declared a variable manager in it which is local to the function manager()
  • 10. 10 #include<stdio.h> #include<conio.h> struct employees { char name; int age; int salary; }; int manager() { struct employees manager; manager.age=28; if(manager.age>30) manager.salary=65000; else manager.salary=55000; return manager.salary; } int main() { struct employees employee1; struct employees employee2; printf(“Enter the salary of employee1:”); scanf(“%d”, &employee1.salary); printf(“Enter the salary of employee2:”); scanf(“%d”, &employee2.salary); printf(“ Employee 1 salary is: %dn”,employee1.salary); printf(“ Employee 2 salary is: %dn”,employee2.salary); printf(“ Manager’s salary is: %dn”,manager()); return 0; } OUTPUT Enter the salary of employee1: 35000 Enter the salary of employee2: 20000 Employee 1 salary is: 35000 Employee 2 salary is: 20000 Manager’s salary is: 55000 PROGRAM SHOWING STRUCTURE AS FUNCTION ARGUMENTS
  • 11. Array of Structures in C An array of structures in C can be defined as the collection of multiple structures variables where each variable contains information about different entities. The array of structures in C are used to store information about multiple entities of different data types. The array of structures is also known as the collection of structures. Once a structure has been defined, we can declare an array of structures for example; struct student class[50]; The individual members can be accessed as class[i].name class[5].roll_number
  • 12. Example of an array of structures that stores information of 3 students and prints it. #include<stdio.h> #include <string.h> struct student{ int rollno; char name[10]; }; int main() { int i; struct student st[3]; printf("Enter Records of 3 students"); for(i=0;i<3;i++) { printf("nEnter Rollno:"); scanf("%d",&st[i].rollno); printf("nEnter Name:"); scanf("%s",&st[i].name); } printf("nStudent Information List:"); for(i=0;i<3;i++){ printf("nRollno:%d, Name:%s",st[i].rollno,st[i].name); } return 0; } OUTPUT Enter Records of 3 students Enter Rollno: 1 Enter Name: Jamie Enter Rollno: 2 Enter Name: Ratan Enter Rollno: 3 Enter Name: John Student Information List: Rollno:1, Name: Jamie Rollno:2, Name: Ratan Rollno:3, Name: John
  • 13. Pointers to Structures 13 struct abc { int x; int y; }; int main() { struct abc a={0,1}; struct abc *ptr=&a; printf(“%d %d”, ptr->x, ptr->y); return 0; } ptr is a pointer to some variable of type struct abc We can define pointers to structures in the same way as we define pointer to any other variable We can store the address of a structure variable (a) in the defined pointer variable(*ptr) To find the address of a structure variable, place the '&'; operator before the structure's name To access the members of a structure using a pointer to that structure, we use the → operator ; 0,1 OUTPUT
  • 14. 14 Nested Structure in C C provides us the feature of nesting one structure within another structure by using which, complex data types are created. Nested structures means, that one structure has another stucture as member variable. struct student { char[30] name; int age; /* here Address is a structure */ struct Address { char[50] locality; char[50] city; int pincode; } addr;