SlideShare a Scribd company logo
CHAPTER 7: STRUCTURES
Outline: Objectives 7.1 Introduction 7.2 Defining a Structure 7.3 Defining a Structure using  typedef 7.4 Structure Members 7.5 Initializing Structures 7.6 Nested Structures 7.7 Array of Structures 7.8 Structures and Pointers 7.9 Passing Structures to Functions
Objectives Defining structures in various ways of structure declaration. Initializing members of a structure variable. Defining element array of structures. Accessing individual structure members. Defining structures using  typedef . Accessing members of a structure using pointer. Passing structures as arguments to functions.
7.1 Introduction A structure can have  individual elements different in type .  contain integer elements, floating-point elements and character elements.  included elements: pointers, arrays and other structures.  The individual structure elements are referred to as  members .
7.2 Defining a Structure General terms: where: struct   is a reserved word. tag  is a name that identifies structures of this type. member1, member2, ..., memberN  are individual member declarations. must  end with semicolon  ( ; ). struct  tag  { member1 ; member2 ; ... memberN ; };
7.2 Defining a Structure – cont’1 The  individual members : can be ordinary variables, pointers, arrays or other structures.  cannot  be  initialized  within a  structure-type  declaration. The  member names: within a particular structure must be  distinct from one another .  can be  the same  as the variable name that is  defined outside  of the structure.  A  storage class   cannot  be  assigned  to an individual member.
7.2 Defining a Structure – cont’2 Example 7.1: Typical structure declarations.  Explanations: Structure named  custAccount . The custAccount contains  4 members  and  2 structure variables . struct custAccount  { int accNo; char accType; char name[80]; float accBalance; }; struct custAccount oldcust, newcust;
7.2 Defining a Structure – cont’3 Example 7.2: This declaration is equivalent with Example 7.1.   struct custAccount  { int accNo; char accType; char name[80]; float accBalance; } oldcust, newcust;
7.2 Defining a Structure – cont’4 Example 7.3: A structure variable may be defined as a member of another structure. struct date { int month; int day; int year; }; struct custAccount { int accNo; char accType; char name[80]; float accBalance; struct date payment; } oldcust, newcust; The  declaration of the embedded structure  must  appear before  the declaration of the outer structure. Eg.: Structure  date  is declared before structure  custAccount .
7.2 Defining a Structure – cont’5
7.3 Defining using  typedef Alternative approach on defining a structure. Made  once at the head  of the program. Subsequent  declarations: using the  new name .
7.3 Defining using  typedef  - cont’1 Features: Allows users to define new data-types that are equivalent to existing data-types. Eliminates the need to repeated write  struct tag  whenever a structure is referenced.
7.3 Defining using  typedef  - cont’2 General user-defined structure type: typedef struct  { member1; member2; ...; memberN; } new_type  The  new_type  refers to the user-defined structure type. Structure variables can be defined in terms of the new data type.
7.3 Defining using  typedef  - cont’3 Example 7.14: Alternative declaration using  typedef . typedef struct { int accNo; char accType; char name[80]; float accBalance; } record; record oldcust, newcust; The  record   is defined as a user-defined data type. The second declaration defines  oldcust  and  newcust  as structure variables of type  record .
7.3 Defining using  typedef  - cont’3 The typedef features can be used  repeatedly , to define one data type in terms of other user-defined data types.  Example 7.5 shows: the  date  and  record  are user-defined structure types. The  cust  is a 100-element array of type  record . typedef struct { int month; int day; int year; } date; typedef struct { int accNo; char accType; char name[80]; float accBalance; date payment; } record; record cust[100];
7.3 Defining using  typedef  - cont’4 Exp. 7.6 and Exp. 7.7 shows the variation of Exp. 7.5. typedef struct { int month; int day; int year; } date; typedef struct { int accNo; char accType; char name[80]; float accBalance; date payment; } record[100]; record cust; typedef struct { int month; int day; int year; } date; struct { int accNo; char accType; char name[80]; float accBalance; date payment; } cust[100];
7.4 Structure Members The members of a structure are usually  processed individually  to access the individual structure members.  It can be accessed by: variable.member variables’ name act as a separator members’ name
7.4 Structure Members  - cont’1 Example 7.8  struct date { int month; int day; int year; }; struct custAccount{ int accNo; char accType; char name[80]; float accBalance; struct date payment; } cust; Example: To access the info. of cust. /* to access the customer’s */ /* account no. */ cust.accNo /* to access the customer’s */ /* name */ cust.name cust.name[2] &cust.name[2] /* to access the month of */ /* the payment of customer */ cust.payment.month
7.5 Initializing Structures The members of a structure variable can be assigned initial values.  The initial values must appear in the order in which they will be assigned to their corresponding structure members.
7.5 Initializing Structures – cont’1 The general form: storage_class struct tag variable = {value1, value2, ..., valueN};  Refer to the value of corresponding structure members auto, extern, register, static
7.5 Initializing Structures – cont’1 struct date { int month; int day; int year; }; struct custAccount { int accNo; char accType; char name[80]; float accBalance; struct date payment; }; static  struct custAccount cust= {2345,‘S’,“Khadijah Ismail”,2400.90,9,7,2006}; : Example 7.9
7.5 Initializing Structures – cont’2 Example 7.10:
7.6 Nested Stuctures saves time when writing programs that use similar structure.  The common members have to define only once in their own structure and then use that structure as a member in another structure.
7.6 Nested Structures – cont’1 struct employees{ char empName[25]; char  address[30] ; char  city [10] ; char  state[2] ; long int  poscode ; double salary; }; struct customers{ char custName[25]; char  address[30] ; char  city [10] ; char  state[2] ; long int  poscode ; double balance; }; struct addressInfo{ char address[30]; char city [10]; char state[2]; long int poscode; }; struct employees{ char empName[25]; struct addressInfo eAddress; double salary; }e1; struct customers{ char custName[25]; struct addressInfo cAddress; double balance; }c1; Example 7.12: :Example 7.13
7.7 Arrays of Structures Example 7.14: Declaration of a 200-element array of structures. struct date { int month; int day; int year; }; struct custAccount{ int accNo; char accType; char name[80]; float accBalance; struct date payment; } cust[200]; struct date { int month; int day; int year; }; struct custAccount{ int accNo; char accType; char name[80]; float accBalance; struct date payment; }; struct custAccount cust[200]; OR
7.7 Arrays of Structures – cont’2 Example 7.16
7.7 Arrays of Structures – cont’1 Example 7.15: Assigning initial values to an array of structures  struct student { char name[80]; float courseMark; float finalMark; int TotMark; }; static struct student MarkList[ ] = { “ Kadir”, 45.50, 30.00, 76, "Azizan", 43.50, 30.00, 73, "Xavier", 44.50, 30.00, 75, "Nantha", 46.50, 30.00, 77, "Junani", 42.50, 30.00, 73, "Martha", 42.00, 30.00, 72 } ;
7.8 Structures and Pointers A pointer to a structure type variable is declared by a statement: struct name *ptr ; A structure member could be accessed by pointers with the following statements: struct PersonalData *ptr ; (*ptr).YearOfBirth=20 ; struct PersonalData *ptr ; ptr -> YearOfBirth=20 ; OR
7.8 Structures and Pointers – cont’1 Example 7.16: Example 7.17:
7.9 Passing Structures to Func There are several different ways to pass structure-type information to or from a function.  transferred individually , or  entire structures  can be transferred.
7.9 Passing Structures … – cont’1 Individual structure members can be: passed to a function  as arguments  in the function call, and  a single structure member can be  returned  by the  return   statement.  Each structure member is treated the same as an ordinary single-valued variable.
7.9 Passing Structures … – cont’2 Example 7.18: The program transfer partial structure member to a function,  addition.
7.9 Passing Structures … – cont’3 Example 7.19: The program transfer a structure-type pointer to a function,  adjust .

More Related Content

PPT
Ch6 pointers (latest)
PPT
Ch5 array nota
PPT
Structures
PPT
structure and union
PPTX
Module 3-Functions
PPSX
C programming structure & pointer
PPT
Structure and union
PPTX
Intoduction to structure
Ch6 pointers (latest)
Ch5 array nota
Structures
structure and union
Module 3-Functions
C programming structure & pointer
Structure and union
Intoduction to structure

What's hot (20)

PDF
Resource wrappers in C++
PPTX
Module 1:Introduction
PDF
PPT
Ch2 introduction to c
PPTX
Module 5-Structure and Union
PDF
C Prog - Pointers
PPTX
Structure & union
PPTX
Module 4- Arrays and Strings
PPTX
COM1407: Working with Pointers
PPTX
PPT
Ch4 functions
PPTX
Pointers in C Programming
PPT
Pointer in C
PPT
Pointers in C
PDF
Pointers & References in C++
PPT
Advanced Programming C++
PPTX
Pointer in C
PPS
C programming session 04
Resource wrappers in C++
Module 1:Introduction
Ch2 introduction to c
Module 5-Structure and Union
C Prog - Pointers
Structure & union
Module 4- Arrays and Strings
COM1407: Working with Pointers
Ch4 functions
Pointers in C Programming
Pointer in C
Pointers in C
Pointers & References in C++
Advanced Programming C++
Pointer in C
C programming session 04
Ad

Viewers also liked (20)

PDF
Structures in c++
PPT
Ch8 file processing
PPT
Ch3 repetition
PPT
Ch3 selection
PPT
PPT
10 high speedla-ns
PPT
PPT
Ch1 principles of software development
PPT
6 data linkcontrol
PPT
8 spread spectrum
PPT
5 digital datacomm
PPT
12 wireless la-ns
DOCX
AM Receivers
PPT
11 circuit-packet
PPTX
Types of AM Receiver
PPT
Chapter 3 am receivers
PPT
4 signal encodingtechniques
PPT
7 multiplexing
PPT
Chapter 2 amplitude_modulation
PPTX
Protocolos- SMTP, POP3 e IMAP4
Structures in c++
Ch8 file processing
Ch3 repetition
Ch3 selection
10 high speedla-ns
Ch1 principles of software development
6 data linkcontrol
8 spread spectrum
5 digital datacomm
12 wireless la-ns
AM Receivers
11 circuit-packet
Types of AM Receiver
Chapter 3 am receivers
4 signal encodingtechniques
7 multiplexing
Chapter 2 amplitude_modulation
Protocolos- SMTP, POP3 e IMAP4
Ad

Similar to Ch7 structures (20)

DOC
9.structure & union
PPTX
CHAPTER -4-class and structure.pptx
PPTX
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
PPTX
12Structures.pptx
PPT
structure.ppt
PPTX
Programming for problem solving-II(UNIT-2).pptx
PPT
C Language_PPS_3110003_unit 8ClassPPT.ppt
PPTX
Structure in c language
PPTX
structure .pptx
PDF
Pointers and Structures
PPTX
Cs1123 12 structures
PDF
structure1.pdf
PDF
Data Structure & Algorithm - Self Referential
PPT
FP 201 - Unit4 Part 2
PDF
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
PDF
Chapter 7 (Part I) - User Defined Datatypes.pdf
PPTX
data structure and c programing concepts
DOC
Unit 5 (1)
PPTX
Lab 13
PDF
C structure and union
9.structure & union
CHAPTER -4-class and structure.pptx
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
12Structures.pptx
structure.ppt
Programming for problem solving-II(UNIT-2).pptx
C Language_PPS_3110003_unit 8ClassPPT.ppt
Structure in c language
structure .pptx
Pointers and Structures
Cs1123 12 structures
structure1.pdf
Data Structure & Algorithm - Self Referential
FP 201 - Unit4 Part 2
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Chapter 7 (Part I) - User Defined Datatypes.pdf
data structure and c programing concepts
Unit 5 (1)
Lab 13
C structure and union

More from Hattori Sidek (10)

PPT
Chapter 4 frequency modulation
PPT
3. transmission media
PPT
2[1].1 data transmission
PPT
14 congestionin datanetworks
PPT
01 pengenalan
PPT
01 berkenalan
PPT
Comm introduction
PPT
Chapter5 dek3133
PPT
Chapter 6 edit
PPT
Chapter 6 dc motor speed control
Chapter 4 frequency modulation
3. transmission media
2[1].1 data transmission
14 congestionin datanetworks
01 pengenalan
01 berkenalan
Comm introduction
Chapter5 dek3133
Chapter 6 edit
Chapter 6 dc motor speed control

Recently uploaded (20)

PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Pharma ospi slides which help in ospi learning
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Types and Its function , kingdom of life
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Cell Structure & Organelles in detailed.
PPTX
master seminar digital applications in india
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Microbial disease of the cardiovascular and lymphatic systems
Sports Quiz easy sports quiz sports quiz
Renaissance Architecture: A Journey from Faith to Humanism
Pharma ospi slides which help in ospi learning
human mycosis Human fungal infections are called human mycosis..pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Microbial diseases, their pathogenesis and prophylaxis
Cell Types and Its function , kingdom of life
O7-L3 Supply Chain Operations - ICLT Program
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Complications of Minimal Access Surgery at WLH
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Computing-Curriculum for Schools in Ghana
Cell Structure & Organelles in detailed.
master seminar digital applications in india
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Microbial disease of the cardiovascular and lymphatic systems

Ch7 structures

  • 2. Outline: Objectives 7.1 Introduction 7.2 Defining a Structure 7.3 Defining a Structure using typedef 7.4 Structure Members 7.5 Initializing Structures 7.6 Nested Structures 7.7 Array of Structures 7.8 Structures and Pointers 7.9 Passing Structures to Functions
  • 3. Objectives Defining structures in various ways of structure declaration. Initializing members of a structure variable. Defining element array of structures. Accessing individual structure members. Defining structures using typedef . Accessing members of a structure using pointer. Passing structures as arguments to functions.
  • 4. 7.1 Introduction A structure can have individual elements different in type . contain integer elements, floating-point elements and character elements. included elements: pointers, arrays and other structures. The individual structure elements are referred to as members .
  • 5. 7.2 Defining a Structure General terms: where: struct is a reserved word. tag is a name that identifies structures of this type. member1, member2, ..., memberN are individual member declarations. must end with semicolon ( ; ). struct tag { member1 ; member2 ; ... memberN ; };
  • 6. 7.2 Defining a Structure – cont’1 The individual members : can be ordinary variables, pointers, arrays or other structures. cannot be initialized within a structure-type declaration. The member names: within a particular structure must be distinct from one another . can be the same as the variable name that is defined outside of the structure. A storage class cannot be assigned to an individual member.
  • 7. 7.2 Defining a Structure – cont’2 Example 7.1: Typical structure declarations. Explanations: Structure named custAccount . The custAccount contains 4 members and 2 structure variables . struct custAccount { int accNo; char accType; char name[80]; float accBalance; }; struct custAccount oldcust, newcust;
  • 8. 7.2 Defining a Structure – cont’3 Example 7.2: This declaration is equivalent with Example 7.1. struct custAccount { int accNo; char accType; char name[80]; float accBalance; } oldcust, newcust;
  • 9. 7.2 Defining a Structure – cont’4 Example 7.3: A structure variable may be defined as a member of another structure. struct date { int month; int day; int year; }; struct custAccount { int accNo; char accType; char name[80]; float accBalance; struct date payment; } oldcust, newcust; The declaration of the embedded structure must appear before the declaration of the outer structure. Eg.: Structure date is declared before structure custAccount .
  • 10. 7.2 Defining a Structure – cont’5
  • 11. 7.3 Defining using typedef Alternative approach on defining a structure. Made once at the head of the program. Subsequent declarations: using the new name .
  • 12. 7.3 Defining using typedef - cont’1 Features: Allows users to define new data-types that are equivalent to existing data-types. Eliminates the need to repeated write struct tag whenever a structure is referenced.
  • 13. 7.3 Defining using typedef - cont’2 General user-defined structure type: typedef struct { member1; member2; ...; memberN; } new_type The new_type refers to the user-defined structure type. Structure variables can be defined in terms of the new data type.
  • 14. 7.3 Defining using typedef - cont’3 Example 7.14: Alternative declaration using typedef . typedef struct { int accNo; char accType; char name[80]; float accBalance; } record; record oldcust, newcust; The record is defined as a user-defined data type. The second declaration defines oldcust and newcust as structure variables of type record .
  • 15. 7.3 Defining using typedef - cont’3 The typedef features can be used repeatedly , to define one data type in terms of other user-defined data types. Example 7.5 shows: the date and record are user-defined structure types. The cust is a 100-element array of type record . typedef struct { int month; int day; int year; } date; typedef struct { int accNo; char accType; char name[80]; float accBalance; date payment; } record; record cust[100];
  • 16. 7.3 Defining using typedef - cont’4 Exp. 7.6 and Exp. 7.7 shows the variation of Exp. 7.5. typedef struct { int month; int day; int year; } date; typedef struct { int accNo; char accType; char name[80]; float accBalance; date payment; } record[100]; record cust; typedef struct { int month; int day; int year; } date; struct { int accNo; char accType; char name[80]; float accBalance; date payment; } cust[100];
  • 17. 7.4 Structure Members The members of a structure are usually processed individually to access the individual structure members. It can be accessed by: variable.member variables’ name act as a separator members’ name
  • 18. 7.4 Structure Members - cont’1 Example 7.8 struct date { int month; int day; int year; }; struct custAccount{ int accNo; char accType; char name[80]; float accBalance; struct date payment; } cust; Example: To access the info. of cust. /* to access the customer’s */ /* account no. */ cust.accNo /* to access the customer’s */ /* name */ cust.name cust.name[2] &cust.name[2] /* to access the month of */ /* the payment of customer */ cust.payment.month
  • 19. 7.5 Initializing Structures The members of a structure variable can be assigned initial values. The initial values must appear in the order in which they will be assigned to their corresponding structure members.
  • 20. 7.5 Initializing Structures – cont’1 The general form: storage_class struct tag variable = {value1, value2, ..., valueN}; Refer to the value of corresponding structure members auto, extern, register, static
  • 21. 7.5 Initializing Structures – cont’1 struct date { int month; int day; int year; }; struct custAccount { int accNo; char accType; char name[80]; float accBalance; struct date payment; }; static struct custAccount cust= {2345,‘S’,“Khadijah Ismail”,2400.90,9,7,2006}; : Example 7.9
  • 22. 7.5 Initializing Structures – cont’2 Example 7.10:
  • 23. 7.6 Nested Stuctures saves time when writing programs that use similar structure. The common members have to define only once in their own structure and then use that structure as a member in another structure.
  • 24. 7.6 Nested Structures – cont’1 struct employees{ char empName[25]; char address[30] ; char city [10] ; char state[2] ; long int poscode ; double salary; }; struct customers{ char custName[25]; char address[30] ; char city [10] ; char state[2] ; long int poscode ; double balance; }; struct addressInfo{ char address[30]; char city [10]; char state[2]; long int poscode; }; struct employees{ char empName[25]; struct addressInfo eAddress; double salary; }e1; struct customers{ char custName[25]; struct addressInfo cAddress; double balance; }c1; Example 7.12: :Example 7.13
  • 25. 7.7 Arrays of Structures Example 7.14: Declaration of a 200-element array of structures. struct date { int month; int day; int year; }; struct custAccount{ int accNo; char accType; char name[80]; float accBalance; struct date payment; } cust[200]; struct date { int month; int day; int year; }; struct custAccount{ int accNo; char accType; char name[80]; float accBalance; struct date payment; }; struct custAccount cust[200]; OR
  • 26. 7.7 Arrays of Structures – cont’2 Example 7.16
  • 27. 7.7 Arrays of Structures – cont’1 Example 7.15: Assigning initial values to an array of structures struct student { char name[80]; float courseMark; float finalMark; int TotMark; }; static struct student MarkList[ ] = { “ Kadir”, 45.50, 30.00, 76, "Azizan", 43.50, 30.00, 73, "Xavier", 44.50, 30.00, 75, "Nantha", 46.50, 30.00, 77, "Junani", 42.50, 30.00, 73, "Martha", 42.00, 30.00, 72 } ;
  • 28. 7.8 Structures and Pointers A pointer to a structure type variable is declared by a statement: struct name *ptr ; A structure member could be accessed by pointers with the following statements: struct PersonalData *ptr ; (*ptr).YearOfBirth=20 ; struct PersonalData *ptr ; ptr -> YearOfBirth=20 ; OR
  • 29. 7.8 Structures and Pointers – cont’1 Example 7.16: Example 7.17:
  • 30. 7.9 Passing Structures to Func There are several different ways to pass structure-type information to or from a function. transferred individually , or entire structures can be transferred.
  • 31. 7.9 Passing Structures … – cont’1 Individual structure members can be: passed to a function as arguments in the function call, and a single structure member can be returned by the return statement. Each structure member is treated the same as an ordinary single-valued variable.
  • 32. 7.9 Passing Structures … – cont’2 Example 7.18: The program transfer partial structure member to a function, addition.
  • 33. 7.9 Passing Structures … – cont’3 Example 7.19: The program transfer a structure-type pointer to a function, adjust .