SlideShare a Scribd company logo
Dr.J.Kalavathi.,M.Sc.,Ph.D.,
Assistant Professor
Department of Information Technology
V.V.Vanniaperumal College for Women,
Virudhunagar.
What is a structure?
 A structure is a user defined data type.
 A structure creates a data type that can be used to group
items of possibly different types into a single type.
 The variables are called members of the structure.
Structures in c
How to create a structure?
 ‘struct’ keyword is used to create a structure.
Following is an example.
struct address
{
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
How to declare structure
variables?
 A structure variable can either be declared with structure
declaration or as a separate declaration like basic types.
// A variable declaration with structure declaration.
struct Point
{
int x, y;
} p1; // The variable p1 is declared with 'Point‘
How to declare structure
variables?
// A variable declaration like basic data types
struct Point
{
int x, y;
};
int main()
{
struct Point p1; // The variable p1 is declared like a normal
variable
}
How to initialize structure
members?
 Structure members cannot be initialized with declaration.
 For example the following C program fails in compilation.
struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};
 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};
}
How to access structure elements?
 Structure members are accessed using dot (.) operator.
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
p1.x = 20; // Accessing members of point p1
printf ("x = %d, y = %d", p1.x, p1.y);
return 0;
}
Copying and Comparing Structure
Variables
 Two variables of the same structure type can be copied in the
same way as ordinary variables.
 If student1 and student2 belong to the same structure, then the
following statements are valid:
student1=student2;
student2=student1;
 However, the statements such as:
student1==student2
student1!=student2
are not permitted.
• If we need to compare the structure variables, we may do so
by comparing members individually.
What is an array of structures?
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point arr[10]; // Create an Array of Structures
arr[0].x = 10; // Access Array Members
arr[0].y = 20;
printf("%d %d", arr[0].x, arr[0].y);
return 0;
}
Structures in c

More Related Content

PDF
PHP Variable variables Examples
PPTX
Java Basic
PPTX
Jsp session 13
PPTX
C# Basics
PPT
Pemrograman komputer 8 (prosedur)
PPT
Unions in c
PPTX
Constant a,variables and data types
PPTX
Introduction to Java
PHP Variable variables Examples
Java Basic
Jsp session 13
C# Basics
Pemrograman komputer 8 (prosedur)
Unions in c
Constant a,variables and data types
Introduction to Java

Similar to Structures in c (20)

PPTX
Structure in C
PDF
1. structure
PPTX
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
PPTX
Fundamentals of Structure in C Programming
PPTX
Cs1123 12 structures
PPTX
detail structure presentation of problem solving
PPTX
Structure.pptx Structure.pptxStructure.pptxStructure.pptxStructure.pptxStruct...
DOCX
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
PDF
Structures and Pointers
PPTX
User defined data types.pptx
PPTX
12Structures.pptx
PDF
Lk module4 structures
PPTX
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
PPT
PPTX
Structure prespentation
PPTX
Programming for problem solving-II(UNIT-2).pptx
PDF
C structure and union
PPTX
Structures and Unions
PPTX
Presentation on c programing satcture
PPTX
Presentation on c structures
Structure in C
1. structure
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
Fundamentals of Structure in C Programming
Cs1123 12 structures
detail structure presentation of problem solving
Structure.pptx Structure.pptxStructure.pptxStructure.pptxStructure.pptxStruct...
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
Structures and Pointers
User defined data types.pptx
12Structures.pptx
Lk module4 structures
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Structure prespentation
Programming for problem solving-II(UNIT-2).pptx
C structure and union
Structures and Unions
Presentation on c programing satcture
Presentation on c structures
Ad

More from kalavathisugan (13)

PPTX
Serial Communication.pptx
PPTX
Timer and counting.pptx
PPTX
SS-assemblers 1.pptx
PPTX
SS-CISC -1.pptx
PPTX
SS-SIC (1).pptx
PPTX
Chapter 3.4.pptx
PPTX
Cloud Computing 1.3.pptx
PPTX
Cloud computing 2.pptx
PPTX
Data reduction
PPTX
Data integration
PPTX
Data pre processing
PPT
PPTX
Functions in c
Serial Communication.pptx
Timer and counting.pptx
SS-assemblers 1.pptx
SS-CISC -1.pptx
SS-SIC (1).pptx
Chapter 3.4.pptx
Cloud Computing 1.3.pptx
Cloud computing 2.pptx
Data reduction
Data integration
Data pre processing
Functions in c
Ad

Recently uploaded (20)

PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
August Patch Tuesday
PPTX
Spectroscopy.pptx food analysis technology
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Encapsulation theory and applications.pdf
PPTX
A Presentation on Artificial Intelligence
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Machine learning based COVID-19 study performance prediction
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Heart disease approach using modified random forest and particle swarm optimi...
Reach Out and Touch Someone: Haptics and Empathic Computing
Digital-Transformation-Roadmap-for-Companies.pptx
Unlocking AI with Model Context Protocol (MCP)
cloud_computing_Infrastucture_as_cloud_p
Spectral efficient network and resource selection model in 5G networks
Advanced methodologies resolving dimensionality complications for autism neur...
August Patch Tuesday
Spectroscopy.pptx food analysis technology
Univ-Connecticut-ChatGPT-Presentaion.pdf
Encapsulation_ Review paper, used for researhc scholars
Mobile App Security Testing_ A Comprehensive Guide.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Encapsulation theory and applications.pdf
A Presentation on Artificial Intelligence
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Machine learning based COVID-19 study performance prediction
TLE Review Electricity (Electricity).pptx
Building Integrated photovoltaic BIPV_UPV.pdf

Structures in c

  • 1. Dr.J.Kalavathi.,M.Sc.,Ph.D., Assistant Professor Department of Information Technology V.V.Vanniaperumal College for Women, Virudhunagar.
  • 2. What is a structure?  A structure is a user defined data type.  A structure creates a data type that can be used to group items of possibly different types into a single type.  The variables are called members of the structure.
  • 4. How to create a structure?  ‘struct’ keyword is used to create a structure. Following is an example. struct address { char name[50]; char street[100]; char city[50]; char state[20]; int pin; };
  • 5. How to declare structure variables?  A structure variable can either be declared with structure declaration or as a separate declaration like basic types. // A variable declaration with structure declaration. struct Point { int x, y; } p1; // The variable p1 is declared with 'Point‘
  • 6. How to declare structure variables? // A variable declaration like basic data types struct Point { int x, y; }; int main() { struct Point p1; // The variable p1 is declared like a normal variable }
  • 7. How to initialize structure members?  Structure members cannot be initialized with declaration.  For example the following C program fails in compilation. struct Point { int x = 0; // COMPILER ERROR: cannot initialize members here int y = 0; // COMPILER ERROR: cannot initialize members here };
  • 8.  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}; }
  • 9. How to access structure elements?  Structure members are accessed using dot (.) operator. #include<stdio.h> struct Point { int x, y; }; int main() { struct Point p1 = {0, 1}; p1.x = 20; // Accessing members of point p1 printf ("x = %d, y = %d", p1.x, p1.y); return 0; }
  • 10. Copying and Comparing Structure Variables  Two variables of the same structure type can be copied in the same way as ordinary variables.  If student1 and student2 belong to the same structure, then the following statements are valid: student1=student2; student2=student1;  However, the statements such as: student1==student2 student1!=student2 are not permitted. • If we need to compare the structure variables, we may do so by comparing members individually.
  • 11. What is an array of structures? #include<stdio.h> struct Point { int x, y; }; int main() { struct Point arr[10]; // Create an Array of Structures arr[0].x = 10; // Access Array Members arr[0].y = 20; printf("%d %d", arr[0].x, arr[0].y); return 0; }