SlideShare a Scribd company logo
Dr.G.Jasmine Beulah
Dept. Computer Science,
Kristu Jayanti College, Bengaluru
 Structure is a user defined data type. It is a collection of
different data type, to create a new data type.
 For example, You can define your custom type for
storing student record containing name, age and
mobile.
 Creating structure type will allow you to handle all
properties of student with single variable, instead of
handling it separately.
 In short it is a data structure in which we can collect
different types of data into one entity.
Declaration of Structure:
 To declare or define a structure, we
use struct keyword.
 It is a reserved word in the C .
struct structure_name
{
member1_declaration;
member2_declaration;
... ...
memberN_declaration;
} structure_variable;
So, if it is needed to declare student type variable along
with student structure definition we can use this
approach.
struct student
{
char name[40]; // Student name
int age; // Student age
unsigned long mobile; // Student mobile number
}s1;
 Structure members can be initialized using curly braces ‘{}’.
 For example, following is a valid initialization.
struct Point
{
int x, y;
};
int main()
{
// A valid initialization. member x gets value 0 and y
// gets value 1. The order of declaration is followed.
struct Point p1 = {0, 1};
}
Structure members are accessed using dot (.) operator.
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
// Accessing members of point p1
p1.x = 20;
printf ("x = %d, y = %d", p1.x, p1.y);
return 0;
}
Output: x = 20, y = 1
 An array having structure as its base type is known as
an array of structure.
 To create an array of structure, first structure is
declared and then array of structure is declared just like
an ordinary array.
struct employee
{
int emp_id;
char name[20];
char dept[20];
float salary;
};
Then an array of structure can be created like:
struct employee emp[10]; /* This is array of structure */
#include<stdio.h>
/* Declaration of structure */
struct student
{
char name[30];
int roll;
float marks;
};
int main()
{ /* Declaration of array of structure */
struct student s[3];
int i;
for(i=0;i<3;i++)
{
printf("Enter name, roll and marks of student:n");
scanf("%s%d%f",s[i].name, &s[i].roll, &s[i].marks);
}
printf("Inputted details are:n");
for(i=0;i< 3;i++)
{
printf("Name: %sn",s[i].name);
printf("Roll: %dn", s[i].roll);
printf("Marks: %fnn", s[i].marks);
}
return 0;
}
 A structure can also contain array members just like normal
members such int, float
 We can declare an array if we need to store multiple values
inside structure.
 Structure may include as many array members as we
require.
 Syntax for declaring array within structure is not different
than the conventional syntax. The only difference is that it
is declared inside the structure.
 Consider the following example for storing student’s
information where we have declared an array of mark to
store marks of six subjects and display on the screen.
 Since we need to store marks for six subjects, we have to
use looping structure but the same is not true for character
array of one dimension.
struct student
{
int rno;
char name[20]; // Character array
int mark[6]; // Integer Array with six elements
float per; };
void main()
{
struct student s1; // structure variable declaration
int tot = 0;
clrscr(); // individual member initialization.
printf(“n Enter Student Roll No :”);
scanf(“%d”, &s1.rno);
printf(“n Enter Student Name :”);
gets(s1.name);
// read marks for six subjects using loop n
for(i = 0 ; i < 6 ; i++ )
{
printf(“n Enter Subject %d mark :”,i+1);
scanf(“%d”, &s1.mark[i]);
tot = tot + s1.mark[i];
}
// calculate percentage
s1.per = tot/6 ;
printf(“n Roll No : %dt Name : %s”,s1.rno, s1.name);
for( i = 0 ; i < 6 ; i++ ) // display marks
{
printf(“n%d”,s1.mark[i]);
}
printf(“nTotal : %dt Percentage : %.2f”,tot, s1.per );
getch(); }
 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.
union Data {
int i;
float f;
char str[20];
} data;
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
union Data data;
printf( "Memory size occupied by data : %dn", sizeof(data));
return 0;
}
Structures
union Data {
int i;
float f;
char str[20];
};
int main( ) {
union Data data;
data.i = 10;
printf( "data.i : %dn", data.i);
data.f = 220.5;
printf( "data.f : %fn", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %sn", data.str);
return 0;
}
Structure Union
You can use a struct keyword to
define a structure.
You can use a union keyword to
define a union.
Every member within structure is
assigned a unique memory location.
In union, a memory location is
shared by all the data members.
Changing the value of one data
member will not affect other data
members in structure.
Changing the value of one data
member will change the value of
other data members in union.
The total size of the structure is the
sum of the size of every data
member.
The total size of the union is the size
of the largest data member.
You can retrieve any member at a
time.
You can access one member at a time
in the union.
It supports flexible array. It does not support a flexible array.
S.No Structure Union
1 Definition
Structure is heterogenous collection of
data items grouped together under a
single name
Definition
A union is a memory location that
is shared by several variables of
different datatypes
2 Syntax;
struct tagname
{
datatype member1;
datatype member2;
};
Syntax;
union tagname
{
datatype member1;
datatype member2;
};
3 Eg;
struct sample{
int a;
float b;
char c;
};
Eg;
union sample
{
int a;
float b;
char c;
};
4 keyword − struct keyword − union
5 Memory allocation Memory allocation
6 Memory allocated is the sum of sizes of
all datatypes in structure
(Here, 7bytes)
Memory allocated is the
maximum size allocated among
all the datatypes in union
(Here, 4bytes)
Structures

More Related Content

PPT
Vi editor in linux
PPTX
Conceptual Data Modeling
PPTX
Types of keys in dbms
PPT
SQL select statement and functions
PPT
RDBMS.ppt What is RDBMS RDBMS stands for Relational Database Management System.
PPTX
Basics of shell programming
PPT
Function Oriented Design
PPTX
Ch.1 oop introduction, classes and objects
Vi editor in linux
Conceptual Data Modeling
Types of keys in dbms
SQL select statement and functions
RDBMS.ppt What is RDBMS RDBMS stands for Relational Database Management System.
Basics of shell programming
Function Oriented Design
Ch.1 oop introduction, classes and objects

What's hot (20)

PPT
Exception handling and function in python
PPTX
File handling
PPT
Event+driven+programming key+features
PPT
Java: Java Applets
PPTX
pl/sql Procedure
PPTX
Software Verification and Validation
PPTX
Type casting
PPT
Switch statements in Java
PPT
MySql slides (ppt)
PPTX
Conceptual design & ER Model.pptx
PPTX
Operating system components
DOC
Arrays and Strings
PPT
structure and union
PPTX
File Management in Operating System
PPTX
Relational Database.pptx
PPTX
Unit 6. Arrays
PPTX
ER MODEL
PPTX
Lab 3 Introduction to the UML - how to create a use case diagram
PPT
Looping statements in Java
PPT
Constraints In Sql
Exception handling and function in python
File handling
Event+driven+programming key+features
Java: Java Applets
pl/sql Procedure
Software Verification and Validation
Type casting
Switch statements in Java
MySql slides (ppt)
Conceptual design & ER Model.pptx
Operating system components
Arrays and Strings
structure and union
File Management in Operating System
Relational Database.pptx
Unit 6. Arrays
ER MODEL
Lab 3 Introduction to the UML - how to create a use case diagram
Looping statements in Java
Constraints In Sql
Ad

Similar to Structures (20)

PPTX
Structure&amp;union
PDF
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
PPT
Lecture 19 - Struct and Union
DOC
Unit 5 (1)
PPTX
Structure & union
PDF
Principals of Programming in CModule -5.pdf
DOCX
C programming structures &amp; union
PDF
slideset 7 structure and union (1).pdf
PPT
structure and union from C programming Language
PPT
structure and union from c programming language.ppt
PDF
03 structures
PPTX
Programming for problem solving-II(UNIT-2).pptx
PPT
C Language_PPS_3110003_unit 8ClassPPT.ppt
DOCX
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
PPT
Introduction to structures in c lang.ppt
PPT
structures.ppt
PPT
Unit4 C
PPTX
data structure and c programing concepts
PDF
VIT351 Software Development VI Unit4
PPTX
Unit-V.pptx
Structure&amp;union
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
Lecture 19 - Struct and Union
Unit 5 (1)
Structure & union
Principals of Programming in CModule -5.pdf
C programming structures &amp; union
slideset 7 structure and union (1).pdf
structure and union from C programming Language
structure and union from c programming language.ppt
03 structures
Programming for problem solving-II(UNIT-2).pptx
C Language_PPS_3110003_unit 8ClassPPT.ppt
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
Introduction to structures in c lang.ppt
structures.ppt
Unit4 C
data structure and c programing concepts
VIT351 Software Development VI Unit4
Unit-V.pptx
Ad

More from DrJasmineBeulahG (9)

PPTX
File Handling in C.pptx
PPTX
Constants and Unformatted Input Output Functions.pptx
PPTX
Software Testing.pptx
PPT
Software Process Model.ppt
PPTX
NumPy.pptx
PPTX
Exception Handling in Python
PPTX
STUDENT DETAILS DATABASE.pptx
PPTX
Selection Sort.pptx
PPTX
File Handling in C.pptx
Constants and Unformatted Input Output Functions.pptx
Software Testing.pptx
Software Process Model.ppt
NumPy.pptx
Exception Handling in Python
STUDENT DETAILS DATABASE.pptx
Selection Sort.pptx

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PDF
Complications of Minimal Access Surgery at WLH
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
master seminar digital applications in india
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Classroom Observation Tools for Teachers
Computing-Curriculum for Schools in Ghana
Complications of Minimal Access Surgery at WLH
LDMMIA Reiki Yoga Finals Review Spring Summer
Orientation - ARALprogram of Deped to the Parents.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
A systematic review of self-coping strategies used by university students to ...
Final Presentation General Medicine 03-08-2024.pptx
Practical Manual AGRO-233 Principles and Practices of Natural Farming
master seminar digital applications in india
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Paper A Mock Exam 9_ Attempt review.pdf.
Weekly quiz Compilation Jan -July 25.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Final Presentation General Medicine 03-08-2024.pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Microbial diseases, their pathogenesis and prophylaxis
STATICS OF THE RIGID BODIES Hibbelers.pdf
Cell Structure & Organelles in detailed.
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Classroom Observation Tools for Teachers

Structures

  • 1. Dr.G.Jasmine Beulah Dept. Computer Science, Kristu Jayanti College, Bengaluru
  • 2.  Structure is a user defined data type. It is a collection of different data type, to create a new data type.  For example, You can define your custom type for storing student record containing name, age and mobile.  Creating structure type will allow you to handle all properties of student with single variable, instead of handling it separately.  In short it is a data structure in which we can collect different types of data into one entity.
  • 3. Declaration of Structure:  To declare or define a structure, we use struct keyword.  It is a reserved word in the C .
  • 4. struct structure_name { member1_declaration; member2_declaration; ... ... memberN_declaration; } structure_variable; So, if it is needed to declare student type variable along with student structure definition we can use this approach.
  • 5. struct student { char name[40]; // Student name int age; // Student age unsigned long mobile; // Student mobile number }s1;
  • 6.  Structure members can be initialized using curly braces ‘{}’.  For example, following is a valid initialization. struct Point { int x, y; }; int main() { // A valid initialization. member x gets value 0 and y // gets value 1. The order of declaration is followed. struct Point p1 = {0, 1}; }
  • 7. Structure members are accessed using dot (.) operator. #include<stdio.h> struct Point { int x, y; }; int main() { struct Point p1 = {0, 1}; // Accessing members of point p1 p1.x = 20; printf ("x = %d, y = %d", p1.x, p1.y); return 0; } Output: x = 20, y = 1
  • 8.  An array having structure as its base type is known as an array of structure.  To create an array of structure, first structure is declared and then array of structure is declared just like an ordinary array.
  • 9. struct employee { int emp_id; char name[20]; char dept[20]; float salary; }; Then an array of structure can be created like: struct employee emp[10]; /* This is array of structure */
  • 10. #include<stdio.h> /* Declaration of structure */ struct student { char name[30]; int roll; float marks; };
  • 11. int main() { /* Declaration of array of structure */ struct student s[3]; int i; for(i=0;i<3;i++) { printf("Enter name, roll and marks of student:n"); scanf("%s%d%f",s[i].name, &s[i].roll, &s[i].marks); }
  • 12. printf("Inputted details are:n"); for(i=0;i< 3;i++) { printf("Name: %sn",s[i].name); printf("Roll: %dn", s[i].roll); printf("Marks: %fnn", s[i].marks); } return 0; }
  • 13.  A structure can also contain array members just like normal members such int, float  We can declare an array if we need to store multiple values inside structure.  Structure may include as many array members as we require.  Syntax for declaring array within structure is not different than the conventional syntax. The only difference is that it is declared inside the structure.  Consider the following example for storing student’s information where we have declared an array of mark to store marks of six subjects and display on the screen.  Since we need to store marks for six subjects, we have to use looping structure but the same is not true for character array of one dimension.
  • 14. struct student { int rno; char name[20]; // Character array int mark[6]; // Integer Array with six elements float per; }; void main() { struct student s1; // structure variable declaration int tot = 0; clrscr(); // individual member initialization. printf(“n Enter Student Roll No :”); scanf(“%d”, &s1.rno); printf(“n Enter Student Name :”); gets(s1.name);
  • 15. // read marks for six subjects using loop n for(i = 0 ; i < 6 ; i++ ) { printf(“n Enter Subject %d mark :”,i+1); scanf(“%d”, &s1.mark[i]); tot = tot + s1.mark[i]; } // calculate percentage s1.per = tot/6 ; printf(“n Roll No : %dt Name : %s”,s1.rno, s1.name); for( i = 0 ; i < 6 ; i++ ) // display marks { printf(“n%d”,s1.mark[i]); } printf(“nTotal : %dt Percentage : %.2f”,tot, s1.per ); getch(); }
  • 16.  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. union Data { int i; float f; char str[20]; } data;
  • 17. #include <stdio.h> #include <string.h> union Data { int i; float f; char str[20]; }; int main( ) { union Data data; printf( "Memory size occupied by data : %dn", sizeof(data)); return 0; }
  • 19. union Data { int i; float f; char str[20]; }; int main( ) { union Data data; data.i = 10; printf( "data.i : %dn", data.i); data.f = 220.5; printf( "data.f : %fn", data.f); strcpy( data.str, "C Programming"); printf( "data.str : %sn", data.str); return 0; }
  • 20. Structure Union You can use a struct keyword to define a structure. You can use a union keyword to define a union. Every member within structure is assigned a unique memory location. In union, a memory location is shared by all the data members. Changing the value of one data member will not affect other data members in structure. Changing the value of one data member will change the value of other data members in union. The total size of the structure is the sum of the size of every data member. The total size of the union is the size of the largest data member. You can retrieve any member at a time. You can access one member at a time in the union. It supports flexible array. It does not support a flexible array.
  • 21. S.No Structure Union 1 Definition Structure is heterogenous collection of data items grouped together under a single name Definition A union is a memory location that is shared by several variables of different datatypes 2 Syntax; struct tagname { datatype member1; datatype member2; }; Syntax; union tagname { datatype member1; datatype member2; };
  • 22. 3 Eg; struct sample{ int a; float b; char c; }; Eg; union sample { int a; float b; char c; }; 4 keyword − struct keyword − union 5 Memory allocation Memory allocation 6 Memory allocated is the sum of sizes of all datatypes in structure (Here, 7bytes) Memory allocated is the maximum size allocated among all the datatypes in union (Here, 4bytes)