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

PPTX
HE NE LASER PRESENTATION
PPTX
Structure in C
PPTX
Structures in c language
PPTX
Presentation on c structures
PPTX
Linked list
PPT
Propositional Logic and Pridicate logic
PPTX
Binary search tree deletion
PPT
Introduction & history of dbms
HE NE LASER PRESENTATION
Structure in C
Structures in c language
Presentation on c structures
Linked list
Propositional Logic and Pridicate logic
Binary search tree deletion
Introduction & history of dbms

What's hot (20)

PPTX
Pointers in c language
PPT
structure and union
PPTX
Functions in c language
PPTX
Union in c language
PPTX
Union in C programming
PPT
Structure in C
PPTX
Function in C program
PPTX
Pointers in C Programming
PPTX
Structure in c language
PPTX
classes and objects in C++
PPTX
Arrays in c
PPTX
Pointer in c program
PPTX
Typedef
PDF
Pointers in C
PPTX
Presentation on pointer.
PPTX
String in c programming
PPTX
Pointers in c++
PPTX
PPT
Function overloading(c++)
PPTX
Constructor and Types of Constructors
Pointers in c language
structure and union
Functions in c language
Union in c language
Union in C programming
Structure in C
Function in C program
Pointers in C Programming
Structure in c language
classes and objects in C++
Arrays in c
Pointer in c program
Typedef
Pointers in C
Presentation on pointer.
String in c programming
Pointers in c++
Function overloading(c++)
Constructor and Types of Constructors
Ad

Viewers also liked (20)

PPTX
C programming-apurbo datta
PPTX
C programing basic input and output
PPSX
INTRODUCTION TO C PROGRAMMING
PPTX
C language ppt
PPT
Basics of C programming
PPTX
C decision making and looping.
PDF
C programing Technical interview
PPSX
C Programing Solve Presentation -CSE
PDF
Phần 10: Dữ liệu kiểu cấu trúc
PPT
Ch10
PPTX
Object Oriented Programing in JavaScript
PPT
C language control statements
PPT
ppt on linux by MUKESH PATEL
DOC
Lập trình c++ có lời giải 2
PPT
Decision making and branching
PPT
Decision making and looping
PPTX
Loops in C
PPT
Presentation1 linux os
PPT
Beginners PHP Tutorial
C programming-apurbo datta
C programing basic input and output
INTRODUCTION TO C PROGRAMMING
C language ppt
Basics of C programming
C decision making and looping.
C programing Technical interview
C Programing Solve Presentation -CSE
Phần 10: Dữ liệu kiểu cấu trúc
Ch10
Object Oriented Programing in JavaScript
C language control statements
ppt on linux by MUKESH PATEL
Lập trình c++ có lời giải 2
Decision making and branching
Decision making and looping
Loops in C
Presentation1 linux os
Beginners PHP Tutorial
Ad

Similar to C programing -Structure (20)

PPTX
Structures in c language
PPT
2 lesson 2 object oriented programming in c++
PDF
Unit 4 qba
PPTX
Unit-V.pptx
PPTX
M-2-Pointers-2.pptx by engineering institutions
PPTX
Structure prespentation
PPTX
CPU : Structures And Unions
PPTX
C Structures and Unions
PDF
Chapter 13.1.9
PDF
Lk module4 structures
DOCX
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
PDF
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
PPTX
Computer science ( Structures In C ) Ppt
PDF
C structure and union
PPTX
Chapter 2 part II array and structure.pptx
PDF
Structures in c++
PPT
PPTX
Structure in C++ in the name of the lord
PPTX
Programming in C session 3
DOCX
Structure and Typedef
Structures in c language
2 lesson 2 object oriented programming in c++
Unit 4 qba
Unit-V.pptx
M-2-Pointers-2.pptx by engineering institutions
Structure prespentation
CPU : Structures And Unions
C Structures and Unions
Chapter 13.1.9
Lk module4 structures
PPS 8.8.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.)
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
Computer science ( Structures In C ) Ppt
C structure and union
Chapter 2 part II array and structure.pptx
Structures in c++
Structure in C++ in the name of the lord
Programming in C session 3
Structure and Typedef

Recently uploaded (20)

PDF
Insiders guide to clinical Medicine.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Lesson notes of climatology university.
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Cell Structure & Organelles in detailed.
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
GDM (1) (1).pptx small presentation for students
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Insiders guide to clinical Medicine.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Lesson notes of climatology university.
Computing-Curriculum for Schools in Ghana
Renaissance Architecture: A Journey from Faith to Humanism
Sports Quiz easy sports quiz sports quiz
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Supply Chain Operations Speaking Notes -ICLT Program
01-Introduction-to-Information-Management.pdf
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Cell Structure & Organelles in detailed.
Module 4: Burden of Disease Tutorial Slides S2 2025
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Classroom Observation Tools for Teachers
GDM (1) (1).pptx small presentation for students
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

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