SlideShare a Scribd company logo
1 of 12
CSC 2O3: COMPUTER PROGRAMMING 1
C Structures
DR(MRS) O.E.OJO
Structure in C
• Arrays allow to define type of variables
• that can hold several data items of the same kind.
• Similarly structure is another user defined data type
available in C
• that allows to combine data items of different kinds.
Structure in C
• Title
• Author
• Subject
• Book ID
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
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];
Defining a Structure
• The structure tag is optional
• and each member definition is a normal variable definition,
• such as int i; or float f; or any other valid variable definition.
• At the end of the structure's definition, before the final semicolon,
• you can specify one or more structure variables but it is optional.
How to declare the Book structure:
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Accessing Structure Members
• member access operator (.): access any member of a
structure
•The member access operator is coded as a period
between the structure variable name and the structure
member.
• The keyword struct is used to define variables of
structure type.
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
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 Language");
strcpy( Book1.author, ”Darasimi Ojo");
strcpy( Book1.subject, ”Structure in C");
Book1.book_id = 203;
/* book 2 specification */
strcpy( Book2.title, ”Numerical Computation");
strcpy( Book2.author, ”Damilare Ojo");
strcpy( Book2.subject, ”Bisection Rule");
Book2.book_id = 271;
C Program example on how to use a structure
/* print Book1 info */
printf( "Book 1 title : %sn", Book1.title);
printf( "Book 1 author : %sn", Book1.author);
printf( "Book 1 subject : %sn", Book1.subject);
printf( "Book 1 book_id : %dn", Book1.book_id);
/* print Book2 info */
printf( "Book 2 title : %sn", Book2.title);
printf( "Book 2 author : %sn", Book2.author);
printf( "Book 2 subject : %sn", Book2.subject);
printf( "Book 2 book_id : %dn", Book2.book_id);
return 0;
}
Output
You can pass a structure as a function argument in the same way as you
pass any other variable or pointer.
Structures as Function
Arguments
#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 Language");
strcpy( Book1.author, "Darasimi Ojo ");
strcpy( Book1.subject, "Structure in C ");
Book1.book_id = 203;
/* book 2 specification */
strcpy( Book2.title, "Numerical Computation ");
strcpy( Book2.author, "Damilare Ojo ");
strcpy( Book2.subject, "Bisection Rule");
Book2.book_id = 271;
/* print Book1 info */
printBook(Book1);
/* print Book2 info */
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);
}
Output
Pointers to Structures
.
struct Books *struct_pointer;
You can define pointers to structures in the same way as you define pointer to any
other variable
.
struct_pointer = &Book1;
Now, you 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

More Related Content

Similar to Structures.pptx

Similar to Structures.pptx (20)

[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
 
Coding - L30-L31-Array of structures.pptx
Coding - L30-L31-Array of structures.pptxCoding - L30-L31-Array of structures.pptx
Coding - L30-L31-Array of structures.pptx
 
structure and union1.pdf
structure and union1.pdfstructure and union1.pdf
structure and union1.pdf
 
SPL 14 | Structures in C
SPL 14 | Structures in CSPL 14 | Structures in C
SPL 14 | Structures in C
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structure in c
Structure in cStructure in c
Structure in c
 
Structures bmn
Structures bmnStructures bmn
Structures bmn
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
 
CP01.pptx
CP01.pptxCP01.pptx
CP01.pptx
 
Structure.pptx
Structure.pptxStructure.pptx
Structure.pptx
 
106da session5 c++
106da session5 c++106da session5 c++
106da session5 c++
 
Structures in c programming
Structures in c programmingStructures in c programming
Structures in c programming
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
 
Structures in C
Structures in CStructures in C
Structures in C
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
structure.ppt
structure.pptstructure.ppt
structure.ppt
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Recently uploaded (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 

Structures.pptx

  • 1. CSC 2O3: COMPUTER PROGRAMMING 1 C Structures DR(MRS) O.E.OJO
  • 2. Structure in C • Arrays allow to define type of variables • that can hold several data items of the same kind. • Similarly structure is another user defined data type available in C • that allows to combine data items of different kinds.
  • 3. Structure in C • Title • Author • Subject • Book ID 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
  • 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]; Defining a Structure
  • 5. • The structure tag is optional • and each member definition is a normal variable definition, • such as int i; or float f; or any other valid variable definition. • At the end of the structure's definition, before the final semicolon, • you can specify one or more structure variables but it is optional.
  • 6. How to declare the Book structure: struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book;
  • 7. Accessing Structure Members • member access operator (.): access any member of a structure •The member access operator is coded as a period between the structure variable name and the structure member. • The keyword struct is used to define variables of structure type.
  • 8. #include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; 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 Language"); strcpy( Book1.author, ”Darasimi Ojo"); strcpy( Book1.subject, ”Structure in C"); Book1.book_id = 203; /* book 2 specification */ strcpy( Book2.title, ”Numerical Computation"); strcpy( Book2.author, ”Damilare Ojo"); strcpy( Book2.subject, ”Bisection Rule"); Book2.book_id = 271; C Program example on how to use a structure
  • 9. /* print Book1 info */ printf( "Book 1 title : %sn", Book1.title); printf( "Book 1 author : %sn", Book1.author); printf( "Book 1 subject : %sn", Book1.subject); printf( "Book 1 book_id : %dn", Book1.book_id); /* print Book2 info */ printf( "Book 2 title : %sn", Book2.title); printf( "Book 2 author : %sn", Book2.author); printf( "Book 2 subject : %sn", Book2.subject); printf( "Book 2 book_id : %dn", Book2.book_id); return 0; } Output
  • 10. You can pass a structure as a function argument in the same way as you pass any other variable or pointer. Structures as Function Arguments #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 */
  • 11. /* book 1 specification */ strcpy( Book1.title, "C Programming Language"); strcpy( Book1.author, "Darasimi Ojo "); strcpy( Book1.subject, "Structure in C "); Book1.book_id = 203; /* book 2 specification */ strcpy( Book2.title, "Numerical Computation "); strcpy( Book2.author, "Damilare Ojo "); strcpy( Book2.subject, "Bisection Rule"); Book2.book_id = 271; /* print Book1 info */ printBook(Book1); /* print Book2 info */ 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); } Output
  • 12. Pointers to Structures . struct Books *struct_pointer; You can define pointers to structures in the same way as you define pointer to any other variable . struct_pointer = &Book1; Now, you 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