SlideShare a Scribd company logo
3
Most read
4
Most read
10
Most read
and STRUCTURE
1
Contents:
 Introduction
 Syntax and formation
 Variables and member accessing
 Nesting of structures
 Use of function and pointer in structure
2
Intro:
 Structure is another user defined data type like array
available in C , that allows to combine data items of
different kinds of data types, whereas arrays allow
only of same data type .
 Structures are used to represent a record. Suppose you
want to keep track of your books in a library. You might
want to track the following attributes about each book
− Title
− Author
− Subject
− Book ID
3
All these data can be saved under a
single name in STRUCTURE
Structure declarations
4
To define a structure, you must use the struct statement. The struct statement
defines a new data type, with more than one member. The format of the struct
statement is as follows −
struct [structure tag] {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
struct person
{
char name[50];
int cit_no;
float salary;
};
We can create the structure for a
person as mentioned above as:
Structure variable declaration
5
When a structure is defined, it creates a user-defined type
but, no storage is allocated. For the above structure of person,
variable can be declared as:
struct person
{
char name[50];
int cit_no;
float salary;
};
Inside main function:
struct person p1, p2, p[20];
Another way of creating sturcture variable
is: struct person
{
char name[50];
int cit_no;
float salary;
}p1 ,p2 ,p[20];
Member access
6
Accessing members of a structure
There are two types of operators used for accessing members
of a structure.
1. Member operator(.)
2. Structure pointer operator(->)
Any member of a structure can be accessed as:
structure_variable_name.member_name
Suppose, we want to access salary for variable p2. Then, it can
be accessed as:
p2.salary
Nested Structures
7
Structures can be nested within other structures in C
programming.
struct complex
{
int imag_value;
float real_value;
};
struct number{
struct complex c1;
int real;
}n1,n2;
Suppose you want to access imag_value for n2 structure
variable then,
structure member n1.c1.imag_value is used.
Sample program
CS 3090: Safety Critical Programming in C 8
Pointers to Structure :
9
We can define pointers to structures in the same way as we
define pointer to any other variable −
struct Books *struct_pointer;
Now, we can store the address of a structure variable in the
above defined pointer variable. To find the address of a
structure variable, place the '&'; operator before the
structure's name as follows −
struct_pointer = &Book1;
To access the members of a structure using a pointer to
that structure, you must use the -> operator as follows −
struct_pointer->title;
Example using function and pointer:
10
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books *book );
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
Example using function and pointer:
11
/* print Book1 info by passing address of Book1 */
printBook( &Book1 );
/* print Book2 info by passing address of Book2 */
printBook( &Book2 );
return 0;
}
void printBook( struct Books *book ) {
printf( "Book title : %sn", book->title);
printf( "Book author : %sn", book->author);
printf( "Book subject : %sn", book->subject);
printf( "Book book_id : %dn", book->book_id);
}
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700
OUTPUT
12

More Related Content

What's hot (20)

PPTX
Strings in C language
P M Patil
 
PPTX
Structures in c language
tanmaymodi4
 
PPTX
Data types in c++
Venkata.Manish Reddy
 
PPTX
Strings in C
Kamal Acharya
 
PPTX
Pointer in c
lavanya marichamy
 
PPTX
Structure in C
Kamal Acharya
 
PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
PPT
Structure in c
Prabhu Govind
 
PPTX
Functions in c language
tanmaymodi4
 
PPTX
Type casting in java
Farooq Baloch
 
PPT
File handling in c
David Livingston J
 
PPT
Pointers C programming
Appili Vamsi Krishna
 
PPTX
Structure in c language
sangrampatil81
 
PPTX
Array in c++
Mahesha Mano
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PPTX
Call by value
Dharani G
 
PPT
C++ classes tutorials
Mayank Jain
 
PPTX
Storage class in C Language
Nitesh Kumar Pandey
 
PPTX
Cursors
Priyanka Yadav
 
Strings in C language
P M Patil
 
Structures in c language
tanmaymodi4
 
Data types in c++
Venkata.Manish Reddy
 
Strings in C
Kamal Acharya
 
Pointer in c
lavanya marichamy
 
Structure in C
Kamal Acharya
 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Structure in c
Prabhu Govind
 
Functions in c language
tanmaymodi4
 
Type casting in java
Farooq Baloch
 
File handling in c
David Livingston J
 
Pointers C programming
Appili Vamsi Krishna
 
Structure in c language
sangrampatil81
 
Array in c++
Mahesha Mano
 
Function in C program
Nurul Zakiah Zamri Tan
 
Call by value
Dharani G
 
C++ classes tutorials
Mayank Jain
 
Storage class in C Language
Nitesh Kumar Pandey
 

Viewers also liked (20)

PPTX
C programming-apurbo datta
Apurbo Datta
 
PPTX
C programing basic input and output
dhanajeyan dhanaj
 
PPSX
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi
 
PPTX
C language ppt
Ğäùråv Júñêjå
 
PPT
Basics of C programming
avikdhupar
 
PPTX
C decision making and looping.
Haard Shah
 
PDF
C programing Technical interview
Vishnu Teraiya
 
PPSX
C Programing Solve Presentation -CSE
salman ahmed
 
PDF
Phần 10: Dữ liệu kiểu cấu trúc
Huy Rùa
 
PPT
Ch10
jashliao
 
PPTX
Object Oriented Programing in JavaScript
Akshay Mathur
 
PPT
C language control statements
suman Aggarwal
 
PPT
ppt on linux by MUKESH PATEL
neo_patel
 
DOC
Lập trình c++ có lời giải 2
Minh Ngoc Tran
 
PPT
Decision making and branching
Hossain Md Shakhawat
 
PPT
Decision making and looping
Hossain Md Shakhawat
 
PPTX
Loops in C
Kamal Acharya
 
PPT
Presentation1 linux os
joycoronado
 
PPT
Beginners PHP Tutorial
alexjones89
 
C programming-apurbo datta
Apurbo Datta
 
C programing basic input and output
dhanajeyan dhanaj
 
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi
 
C language ppt
Ğäùråv Júñêjå
 
Basics of C programming
avikdhupar
 
C decision making and looping.
Haard Shah
 
C programing Technical interview
Vishnu Teraiya
 
C Programing Solve Presentation -CSE
salman ahmed
 
Phần 10: Dữ liệu kiểu cấu trúc
Huy Rùa
 
Ch10
jashliao
 
Object Oriented Programing in JavaScript
Akshay Mathur
 
C language control statements
suman Aggarwal
 
ppt on linux by MUKESH PATEL
neo_patel
 
Lập trình c++ có lời giải 2
Minh Ngoc Tran
 
Decision making and branching
Hossain Md Shakhawat
 
Decision making and looping
Hossain Md Shakhawat
 
Loops in C
Kamal Acharya
 
Presentation1 linux os
joycoronado
 
Beginners PHP Tutorial
alexjones89
 
Ad

Similar to C programing -Structure (20)

DOCX
C programming structures &amp; union
Bathshebaparimala
 
PPTX
detail structure presentation of problem solving
talencorconsultancy
 
PDF
Unit 3
TPLatchoumi
 
PPTX
Module 5-Structure and Union
nikshaikh786
 
PPTX
Programming for problem solving-II(UNIT-2).pptx
prathima304
 
PDF
Lk module4 structures
Krishna Nanda
 
PPTX
STRUCTURES IN C PROGRAMMING
Gurwinderkaur45
 
PPT
Session 6
Shailendra Mathur
 
PPTX
Structures.pptx
OluwafolakeOjo
 
PDF
Chapter 13.1.9
patcha535
 
PPT
C Language_PPS_3110003_unit 8ClassPPT.ppt
NikeshaPatel1
 
PPTX
Programming in C session 3
Prerna Sharma
 
PPT
structure.ppt
Sheik Mohideen
 
PPTX
Structures in C.pptx
Boni Yeamin
 
PPTX
Pointers and Structures.pptx
Sharon Manmothe
 
PDF
Structures and Pointers
Prabu U
 
PDF
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
sudhakargeruganti
 
PPTX
Structures in C
Nimrita Koul
 
PPTX
Presentation on c programing satcture
topu93
 
PPTX
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
mahmoud2005rare
 
C programming structures &amp; union
Bathshebaparimala
 
detail structure presentation of problem solving
talencorconsultancy
 
Unit 3
TPLatchoumi
 
Module 5-Structure and Union
nikshaikh786
 
Programming for problem solving-II(UNIT-2).pptx
prathima304
 
Lk module4 structures
Krishna Nanda
 
STRUCTURES IN C PROGRAMMING
Gurwinderkaur45
 
Structures.pptx
OluwafolakeOjo
 
Chapter 13.1.9
patcha535
 
C Language_PPS_3110003_unit 8ClassPPT.ppt
NikeshaPatel1
 
Programming in C session 3
Prerna Sharma
 
structure.ppt
Sheik Mohideen
 
Structures in C.pptx
Boni Yeamin
 
Pointers and Structures.pptx
Sharon Manmothe
 
Structures and Pointers
Prabu U
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
sudhakargeruganti
 
Structures in C
Nimrita Koul
 
Presentation on c programing satcture
topu93
 
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
mahmoud2005rare
 
Ad

Recently uploaded (20)

PPTX
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
 
PPTX
Peer Teaching Observations During School Internship
AjayaMohanty7
 
PDF
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
PPTX
Photo chemistry Power Point Presentation
mprpgcwa2024
 
PPTX
JSON, XML and Data Science introduction.pptx
Ramakrishna Reddy Bijjam
 
PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PDF
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
PPTX
How to Add New Item in CogMenu in Odoo 18
Celine George
 
PPTX
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
PPTX
Comparing Translational and Rotational Motion.pptx
AngeliqueTolentinoDe
 
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
PDF
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
PPTX
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
DOCX
DLL english grade five goof for one week
FlordelynGonzales1
 
PPTX
How to use _name_search() method in Odoo 18
Celine George
 
PDF
VCE Literature Section A Exam Response Guide
jpinnuck
 
PDF
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
PPTX
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
PPTX
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 
PDF
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
 
Peer Teaching Observations During School Internship
AjayaMohanty7
 
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
Photo chemistry Power Point Presentation
mprpgcwa2024
 
JSON, XML and Data Science introduction.pptx
Ramakrishna Reddy Bijjam
 
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
How to Add New Item in CogMenu in Odoo 18
Celine George
 
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
Comparing Translational and Rotational Motion.pptx
AngeliqueTolentinoDe
 
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
DLL english grade five goof for one week
FlordelynGonzales1
 
How to use _name_search() method in Odoo 18
Celine George
 
VCE Literature Section A Exam Response Guide
jpinnuck
 
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 

C programing -Structure

  • 2. Contents:  Introduction  Syntax and formation  Variables and member accessing  Nesting of structures  Use of function and pointer in structure 2
  • 3. Intro:  Structure is another user defined data type like array available in C , that allows to combine data items of different kinds of data types, whereas arrays allow only of same data type .  Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book − Title − Author − Subject − Book ID 3 All these data can be saved under a single name in STRUCTURE
  • 4. Structure declarations 4 To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows − struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables]; struct person { char name[50]; int cit_no; float salary; }; We can create the structure for a person as mentioned above as:
  • 5. Structure variable declaration 5 When a structure is defined, it creates a user-defined type but, no storage is allocated. For the above structure of person, variable can be declared as: struct person { char name[50]; int cit_no; float salary; }; Inside main function: struct person p1, p2, p[20]; Another way of creating sturcture variable is: struct person { char name[50]; int cit_no; float salary; }p1 ,p2 ,p[20];
  • 6. Member access 6 Accessing members of a structure There are two types of operators used for accessing members of a structure. 1. Member operator(.) 2. Structure pointer operator(->) Any member of a structure can be accessed as: structure_variable_name.member_name Suppose, we want to access salary for variable p2. Then, it can be accessed as: p2.salary
  • 7. Nested Structures 7 Structures can be nested within other structures in C programming. struct complex { int imag_value; float real_value; }; struct number{ struct complex c1; int real; }n1,n2; Suppose you want to access imag_value for n2 structure variable then, structure member n1.c1.imag_value is used.
  • 8. Sample program CS 3090: Safety Critical Programming in C 8
  • 9. Pointers to Structure : 9 We can define pointers to structures in the same way as we define pointer to any other variable − struct Books *struct_pointer; Now, we can store the address of a structure variable in the above defined pointer variable. To find the address of a structure variable, place the '&'; operator before the structure's name as follows − struct_pointer = &Book1; To access the members of a structure using a pointer to that structure, you must use the -> operator as follows − struct_pointer->title;
  • 10. Example using function and pointer: 10 #include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; /* function declaration */ void printBook( struct Books *book ); int main( ) { struct Books Book1; /* Declare Book1 of type Book */ struct Books Book2; /* Declare Book2 of type Book */ /* book 1 specification */ strcpy( Book1.title, "C Programming"); strcpy( Book1.author, "Nuha Ali"); strcpy( Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* book 2 specification */ strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Zara Ali"); strcpy( Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700;
  • 11. Example using function and pointer: 11 /* print Book1 info by passing address of Book1 */ printBook( &Book1 ); /* print Book2 info by passing address of Book2 */ printBook( &Book2 ); return 0; } void printBook( struct Books *book ) { printf( "Book title : %sn", book->title); printf( "Book author : %sn", book->author); printf( "Book subject : %sn", book->subject); printf( "Book book_id : %dn", book->book_id); } Book title : C Programming Book author : Nuha Ali Book subject : C Programming Tutorial Book book_id : 6495407 Book title : Telecom Billing Book author : Zara Ali Book subject : Telecom Billing Tutorial Book book_id : 6495700 OUTPUT
  • 12. 12