Download presentation
Presentation is loading. Please wait.
1
C Programming Lecture-13 Structures
$p!derLabWeb C Programming Lecture-13 Structures
2
What is Structure? We have studied that the array is used to store the values of same datatype but suppose if we to store the data of different datatypes than we can’t use the arrays hence structure helps us to make a collection of data of different datatypes. Therefore, structure is the collection of values of different datatypes.
3
Defining a Structure To define a structure we use the keyword struct Syntax:- struct structure_name{ // list of variables of different datatypes. }; Eg. struct student{ int roll_no; char[80] name; };
4
Declaration of variable of defined structure
Syntax :- struct structure_name var_name; Eg struct student s1; // variable of student structure struct student s[5]; // array of student structure struct student *stu; // pointer to student structure
5
Initialization of variable of defined structure
Syntax :- struct structure_name var_name = { list of values }; Eg struct student s1 = { 1, “justine” }; struct student s1 = { 2, “john” }; struct student s1 = { 3, “monto” };
6
Accessing the Structure Members
To access the variables or members of structure, we have to two ways :- -> when the variable of structure is trying to access the members of structure then we use the dot operator ( . ) Eg. struct student s1; s1.roll_no = 4; s1.name = “aman”;
7
-> when the pointer to structure is trying to access the members of structure then we use the arrow operator ( -> ). Eg. struct student *stu; stu->roll_no = 5; stu->name = “channi”;
8
Structure : User Defined Data Types
Structure is collection of data of different datatypes so we can make many type of structure according to the requirement. And we are defining the structure with the use of primitive datatypes and then we use this structure to make variables. Hence we can say that structure is being used as the user defined datatypes.
9
Passing Structures as Function Arguments
As structure is user defined datatypes we can use them as the normal datatypes. Hence for passing the structure as function arguments is no different. Syntax:- Return_type func_name ( struct struct_name var_name, list of variables ){ // body of function. }
10
Pointers to Structures
Pointer to structure is the term used to make pointer pointing to structure. Syntax:- struct structure_name *var_name ; Eg. struct student *stu;
11
Thank you!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.