SlideShare a Scribd company logo
Structures

1
User-Defined Types
• C provides facilities to define one’s own types.
• These may be a composite of basic types (int,
double, etc) and other user-defined types.
• The most common user-defined type is a structure,
defined by the keyword struct.

2
Structures
• A structure is a collection of one or more variables,
possibly of different types, grouped together under a
single name
• Structures are user-defined aggregate types.
• They assist program organisation by
– Grouping logically related data, and giving this set of
variables a higher-level name and more abstract
representation.
– Reducing the number of parameters that need to be passed
between functions.
– Providing another means to return multiple values from a
function.
3
Structure Syntax
• A structure is defined by the keyword struct followed by a set of
variables enclosed in braces.
• Consider the following structure to represent a person’s details.
struct Personnel {
char name[100];
int age;
double height;
};

• The variables name, age and height are called members of the
structure type Personnel.

4
Declaring Structure Variables
•

There are two ways to define variables of a particular
structure type.
1. Declare them at the structure definition.
struct Personnel {
char name[100];
int age;
double height;
} p1, p2, p3; /* Define 3 variables */

2. Define the variables at some point after the structure
definition.
struct Personnel p1, p2, p3; /* Define 3 variables */
5
Initialising Structure Variables
•

A structure may be initialised when it is
defined using brace notation.
struct Personnel captain = {“Fred”, 37, 1.83};

•

The order of values in the initialise list
matches the order of declarations in the
structure.

6
Accessing Members
• Members of a structure type may be accessed via the
“.” member operator.
struct Personnel captain;

strcpy(captain.name, “Fred”);
captain.age = 37;
captain.height = 1.83;
printf(“%s is %d years old.”,
captain.name, captain.age);

7
Nested Structures
• Structures may be defined inside other
structures.
struct first
{
struct second
{
int a;
}s;
double amount;
}f;

• To access lower-level members, need to use
member operator multiple times.
f.s.a = 2.1;
f.amount = 75.4;
8
Operations on Structures
• Structure types only support one of the operations that are
permitted on primitive types.
– Assignment (ie., copying) is permitted
– All other operations (ie., arithmetic, relational, logical) are not
allowed
struct Personnel p1 = {“Fred”, 37, 1.83};
struct Personnel p2;
p2 = p1;
/* Valid. */
if (p1 == p2)
/* Invalid. Won’t compile. */
printf(“People are equaln");
if (strcmp(p1.name, p2.name) == 0 && /* Valid. */
p1.age == p2.age &&
p1.height == p2.height)
printf("People are equaln");

9
Structures and Functions
• Structures may be passed to functions and returned
from functions.
– Like all variables, they are passed by value

10
Pointers to Structures
• Defining pointers is the same as for variables of
primitive types
struct Personnel captain = {“Fred”, 37, 1.83};
struct Personnel *pp;
pp = &captain;
pp->age = 38; /* captain.age is now 38. */

11
Arrays of Structures
• The definition of arrays of structure types is
the same as for arrays of primitive types.
struct Personnel pa[10];

12
Self-Referential Structures
• A structure may not contain a variable of its own type.
struct Node {
int item;
struct Node n; /* Invalid */
};

• However, a structure may contain a pointer, self referential
structure
struct Node {
int item;
struct Node *pn; /* Valid */
};

13
14
Example: A Linked List
• Linked lists come in two basic varieties: singly linked and
doubly linked.
• We describe here a simple version of a singly linked list.
• List consists of a set of nodes, where each node contains an item
and a pointer to another list node.
struct List {
int item;
struct List *next;
};

• (Here we have chosen an int as the contained item. Any other
type(s) may be used.)
15
Singly Linked List
• List is formed by connecting the pointer of one node to the
address of the next.
• We keep a pointer to the head of the list. This permits traversal.
• The end of the list is marked by a NULL pointer.
• Example, to start at the head of the list and traverse to the end
node:
struct List *node = head;
while (node->next != NULL)
node = node->next;
printf(“Last node item: %d“, node->item);
16
Linked-List Properties
• Linked-Lists are useful because they can be grown (or
shrunk) very easily. Unlike arrays, there are no issues
of reallocating memory and copying data.
• Nodes can even be inserted (or removed) from midway
along the list without difficulty (and efficiently).

17
Adding Nodes to a List
• Show example code for adding a node to the end of the list.
• Show example code for adding a node midway through the
list.

18
Splicing in a New Node
•

Remember that everything is manipulated as an address. Consider the pointer variables, eg.,
–
–
–

node
node->next
newnode

is address 0x4D
is address 0xA1
is address 0xB6

0x4D

•

0xA1

Splicing:
newnode->next = node->next; assign to 0xA1
node->next = newnode;
assign to 0xB6
0xB6

0x4D

0xA1
19

More Related Content

What's hot

C# program structure
C# program structureC# program structure
Structure c
Structure cStructure c
Structure c
thirumalaikumar3
 
Structure in c#
Structure in c#Structure in c#
Structure in c#
Dr.Neeraj Kumar Pandey
 
Unit4 C
Unit4 C Unit4 C
Unit4 C
arnold 7490
 
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Smit Shah
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
Ashim Lamichhane
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
Swarup Kumar Boro
 
Strings in c#
Strings in c#Strings in c#
Strings in c#
Dr.Neeraj Kumar Pandey
 
Constant a,variables and data types
Constant a,variables and data typesConstant a,variables and data types
Constant a,variables and data types
mithilesh kumar
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
eShikshak
 
SPL 14 | Structures in C
SPL 14 | Structures in CSPL 14 | Structures in C
SPL 14 | Structures in C
Mohammad Imam Hossain
 
Structure in c sharp
Structure in c sharpStructure in c sharp
Structure in c sharp
rrajeshvhadlure
 
Learn C# Programming - Structure & Enums
Learn C# Programming - Structure & EnumsLearn C# Programming - Structure & Enums
Learn C# Programming - Structure & Enums
Eng Teong Cheah
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
Shahzad Younas
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
asadsardar
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
Rohit Shrivastava
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
Abhishek Wadhwa
 
Inheritance,constructor,friend function
Inheritance,constructor,friend functionInheritance,constructor,friend function
Inheritance,constructor,friend function
FAKRUL ISLAM
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
PhD Research Scholar
 

What's hot (20)

C# program structure
C# program structureC# program structure
C# program structure
 
Structure c
Structure cStructure c
Structure c
 
Structure in c#
Structure in c#Structure in c#
Structure in c#
 
Unit4 C
Unit4 C Unit4 C
Unit4 C
 
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Strings in c#
Strings in c#Strings in c#
Strings in c#
 
Constant a,variables and data types
Constant a,variables and data typesConstant a,variables and data types
Constant a,variables and data types
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
 
SPL 14 | Structures in C
SPL 14 | Structures in CSPL 14 | Structures in C
SPL 14 | Structures in C
 
Structure in c sharp
Structure in c sharpStructure in c sharp
Structure in c sharp
 
Learn C# Programming - Structure & Enums
Learn C# Programming - Structure & EnumsLearn C# Programming - Structure & Enums
Learn C# Programming - Structure & Enums
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Inheritance,constructor,friend function
Inheritance,constructor,friend functionInheritance,constructor,friend function
Inheritance,constructor,friend function
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 

Viewers also liked

Lec 04. If-Else Statement / Increment and Decrement Operators
Lec 04. If-Else Statement / Increment and Decrement OperatorsLec 04. If-Else Statement / Increment and Decrement Operators
Lec 04. If-Else Statement / Increment and Decrement Operators
Rushdi Shams
 
Lec 13. Introduction to Pointers
Lec 13. Introduction to PointersLec 13. Introduction to Pointers
Lec 13. Introduction to Pointers
Rushdi Shams
 
Lec 18. Recursion
Lec 18. RecursionLec 18. Recursion
Lec 18. Recursion
Rushdi Shams
 
Pointers
PointersPointers
Pointers
Frijo Francis
 
Type conversion
Type conversionType conversion
Type conversion
Frijo Francis
 

Viewers also liked (7)

Lec 04. If-Else Statement / Increment and Decrement Operators
Lec 04. If-Else Statement / Increment and Decrement OperatorsLec 04. If-Else Statement / Increment and Decrement Operators
Lec 04. If-Else Statement / Increment and Decrement Operators
 
Lec 13. Introduction to Pointers
Lec 13. Introduction to PointersLec 13. Introduction to Pointers
Lec 13. Introduction to Pointers
 
Lec 18. Recursion
Lec 18. RecursionLec 18. Recursion
Lec 18. Recursion
 
Recursion prog (1)
Recursion prog (1)Recursion prog (1)
Recursion prog (1)
 
Recursion prog
Recursion progRecursion prog
Recursion prog
 
Pointers
PointersPointers
Pointers
 
Type conversion
Type conversionType conversion
Type conversion
 

Similar to Structure

User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
Ananthi Palanisamy
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and Pointers
Prabu U
 
1. structure
1. structure1. structure
1. structure
hasan Mohammad
 
Structures in C
Structures in CStructures in C
Structures in C
Nimrita Koul
 
Fundamentals of Structure in C Programming
Fundamentals of Structure in C ProgrammingFundamentals of Structure in C Programming
Fundamentals of Structure in C Programming
Chandrakant Divate
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
Sowri Rajan
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
GIRISHKUMARBC1
 
DS_PPT.ppt
DS_PPT.pptDS_PPT.ppt
DS_PPT.ppt
MeghaKulkarni27
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
Gem WeBlog
 
C++ training
C++ training C++ training
C++ training
PL Sharma
 
Structure in C
Structure in CStructure in C
Structure in C
Fazle Rabbi Ador
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
WondimuBantihun1
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
Prerna Sharma
 
Session 6
Session 6Session 6
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
mrecedu
 
Data structure
 Data structure Data structure
Data structure
Shahariar limon
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
Swarup Boro
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 
SPC Unit 5
SPC Unit 5SPC Unit 5
SPC Unit 5
SIMONTHOMAS S
 
Structure & union
Structure & unionStructure & union
Structure & union
lalithambiga kamaraj
 

Similar to Structure (20)

User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and Pointers
 
1. structure
1. structure1. structure
1. structure
 
Structures in C
Structures in CStructures in C
Structures in C
 
Fundamentals of Structure in C Programming
Fundamentals of Structure in C ProgrammingFundamentals of Structure in C Programming
Fundamentals of Structure in C Programming
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
 
DS_PPT.ppt
DS_PPT.pptDS_PPT.ppt
DS_PPT.ppt
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
C++ training
C++ training C++ training
C++ training
 
Structure in C
Structure in CStructure in C
Structure in C
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
 
Session 6
Session 6Session 6
Session 6
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
 
Data structure
 Data structure Data structure
Data structure
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
SPC Unit 5
SPC Unit 5SPC Unit 5
SPC Unit 5
 
Structure & union
Structure & unionStructure & union
Structure & union
 

More from Frijo Francis

Data type
Data typeData type
Data type
Frijo Francis
 
C programming language
C programming languageC programming language
C programming language
Frijo Francis
 
Break and continue
Break and continueBreak and continue
Break and continue
Frijo Francis
 
6 enumerated, typedef
6 enumerated, typedef6 enumerated, typedef
6 enumerated, typedef
Frijo Francis
 
5bit field
5bit field5bit field
5bit field
Frijo Francis
 
4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocation
Frijo Francis
 
Union
UnionUnion
1file handling
1file handling1file handling
1file handling
Frijo Francis
 

More from Frijo Francis (8)

Data type
Data typeData type
Data type
 
C programming language
C programming languageC programming language
C programming language
 
Break and continue
Break and continueBreak and continue
Break and continue
 
6 enumerated, typedef
6 enumerated, typedef6 enumerated, typedef
6 enumerated, typedef
 
5bit field
5bit field5bit field
5bit field
 
4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocation
 
Union
UnionUnion
Union
 
1file handling
1file handling1file handling
1file handling
 

Recently uploaded

Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 

Recently uploaded (20)

Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 

Structure

  • 2. User-Defined Types • C provides facilities to define one’s own types. • These may be a composite of basic types (int, double, etc) and other user-defined types. • The most common user-defined type is a structure, defined by the keyword struct. 2
  • 3. Structures • A structure is a collection of one or more variables, possibly of different types, grouped together under a single name • Structures are user-defined aggregate types. • They assist program organisation by – Grouping logically related data, and giving this set of variables a higher-level name and more abstract representation. – Reducing the number of parameters that need to be passed between functions. – Providing another means to return multiple values from a function. 3
  • 4. Structure Syntax • A structure is defined by the keyword struct followed by a set of variables enclosed in braces. • Consider the following structure to represent a person’s details. struct Personnel { char name[100]; int age; double height; }; • The variables name, age and height are called members of the structure type Personnel. 4
  • 5. Declaring Structure Variables • There are two ways to define variables of a particular structure type. 1. Declare them at the structure definition. struct Personnel { char name[100]; int age; double height; } p1, p2, p3; /* Define 3 variables */ 2. Define the variables at some point after the structure definition. struct Personnel p1, p2, p3; /* Define 3 variables */ 5
  • 6. Initialising Structure Variables • A structure may be initialised when it is defined using brace notation. struct Personnel captain = {“Fred”, 37, 1.83}; • The order of values in the initialise list matches the order of declarations in the structure. 6
  • 7. Accessing Members • Members of a structure type may be accessed via the “.” member operator. struct Personnel captain; strcpy(captain.name, “Fred”); captain.age = 37; captain.height = 1.83; printf(“%s is %d years old.”, captain.name, captain.age); 7
  • 8. Nested Structures • Structures may be defined inside other structures. struct first { struct second { int a; }s; double amount; }f; • To access lower-level members, need to use member operator multiple times. f.s.a = 2.1; f.amount = 75.4; 8
  • 9. Operations on Structures • Structure types only support one of the operations that are permitted on primitive types. – Assignment (ie., copying) is permitted – All other operations (ie., arithmetic, relational, logical) are not allowed struct Personnel p1 = {“Fred”, 37, 1.83}; struct Personnel p2; p2 = p1; /* Valid. */ if (p1 == p2) /* Invalid. Won’t compile. */ printf(“People are equaln"); if (strcmp(p1.name, p2.name) == 0 && /* Valid. */ p1.age == p2.age && p1.height == p2.height) printf("People are equaln"); 9
  • 10. Structures and Functions • Structures may be passed to functions and returned from functions. – Like all variables, they are passed by value 10
  • 11. Pointers to Structures • Defining pointers is the same as for variables of primitive types struct Personnel captain = {“Fred”, 37, 1.83}; struct Personnel *pp; pp = &captain; pp->age = 38; /* captain.age is now 38. */ 11
  • 12. Arrays of Structures • The definition of arrays of structure types is the same as for arrays of primitive types. struct Personnel pa[10]; 12
  • 13. Self-Referential Structures • A structure may not contain a variable of its own type. struct Node { int item; struct Node n; /* Invalid */ }; • However, a structure may contain a pointer, self referential structure struct Node { int item; struct Node *pn; /* Valid */ }; 13
  • 14. 14
  • 15. Example: A Linked List • Linked lists come in two basic varieties: singly linked and doubly linked. • We describe here a simple version of a singly linked list. • List consists of a set of nodes, where each node contains an item and a pointer to another list node. struct List { int item; struct List *next; }; • (Here we have chosen an int as the contained item. Any other type(s) may be used.) 15
  • 16. Singly Linked List • List is formed by connecting the pointer of one node to the address of the next. • We keep a pointer to the head of the list. This permits traversal. • The end of the list is marked by a NULL pointer. • Example, to start at the head of the list and traverse to the end node: struct List *node = head; while (node->next != NULL) node = node->next; printf(“Last node item: %d“, node->item); 16
  • 17. Linked-List Properties • Linked-Lists are useful because they can be grown (or shrunk) very easily. Unlike arrays, there are no issues of reallocating memory and copying data. • Nodes can even be inserted (or removed) from midway along the list without difficulty (and efficiently). 17
  • 18. Adding Nodes to a List • Show example code for adding a node to the end of the list. • Show example code for adding a node midway through the list. 18
  • 19. Splicing in a New Node • Remember that everything is manipulated as an address. Consider the pointer variables, eg., – – – node node->next newnode is address 0x4D is address 0xA1 is address 0xB6 0x4D • 0xA1 Splicing: newnode->next = node->next; assign to 0xA1 node->next = newnode; assign to 0xB6 0xB6 0x4D 0xA1 19