Lecture 9 Structure 1 2 3 4 Concepts


![Concepts by example struct student { int id; char name[20]; }; • The structure Concepts by example struct student { int id; char name[20]; }; • The structure](https://slidetodoc.com/presentation_image_h/2cc89f801df123cc74d0f54235e1a58b/image-3.jpg)



![typedef struct { int id; char name[20]; } student; sizeof(student) is equal to ? typedef struct { int id; char name[20]; } student; sizeof(student) is equal to ?](https://slidetodoc.com/presentation_image_h/2cc89f801df123cc74d0f54235e1a58b/image-7.jpg)











![More examples POINT p[10]; /* declare an array of point types */ POINT *pt; More examples POINT p[10]; /* declare an array of point types */ POINT *pt;](https://slidetodoc.com/presentation_image_h/2cc89f801df123cc74d0f54235e1a58b/image-19.jpg)



- Slides: 22

Lecture 9 Structure 1. 2. 3. 4. Concepts of structure Pointers of structures Structure arrays Pass structure to functions © Oxford University Press 2014. All rights reserved.

1. Concepts of structure • Structure is a user-defined data type that can store a sequence of various data types together • Different from array, array has to be the same data type • Similar to array, data of a structure are stored in consecutive memory space • A structure is declared using the keyword struct followed by a structure name. Syntax: struct-name { data_type var-name; . . . }; © Oxford University Press 2014. All rights reserved.
![Concepts by example struct student int id char name20 The structure Concepts by example struct student { int id; char name[20]; }; • The structure](https://slidetodoc.com/presentation_image_h/2cc89f801df123cc74d0f54235e1a58b/image-3.jpg)
Concepts by example struct student { int id; char name[20]; }; • The structure definition does not allocate any memory. • It just gives a template that conveys to the compiler how the structure is laid out in memory and gives details of the members. • Memory is allocated for the structure when we declare a variable of the structure. Example: define a variable of student by writing struct student 1; © Oxford University Press 2014. All rights reserved.

Typedef Declaration • When we precede a struct name with typedef keyword, then the struct becomes a new type. • For example, consider the following declaration: typedef struct { int id; char name[20]; } student; • Now we can straightaway declare variables of this new data type as we declare variables of type int, float, char, double, etc. • To declare a variable of structure student, we will just write: student 1; © Oxford University Press 2014. All rights reserved.

More examples struct point { int x; int y; } p 1; // declare p 1 struct point p 2; // declare p 2 typedef struct { int x; int y; } POINT; POINT p 1, p 2; © Oxford University Press 2014. All rights reserved.

Size of structure Size of a user defined structure data type is the sum of the sizes of data types in the structure definition. Example: typedef struct { int x; int y; } POINT; POINT p 1, p 2; sizeof(POINT) is equal to size(int) + sizeof(int) © Oxford University Press 2014. All rights reserved.
![typedef struct int id char name20 student sizeofstudent is equal to typedef struct { int id; char name[20]; } student; sizeof(student) is equal to ?](https://slidetodoc.com/presentation_image_h/2cc89f801df123cc74d0f54235e1a58b/image-7.jpg)
typedef struct { int id; char name[20]; } student; sizeof(student) is equal to ? © Oxford University Press 2014. All rights reserved.

Initializing Structures • The general syntax to initialize a structure variable is given as follows: struct_name { data_type member_name 1; data_type member_name 2; data_type member_name 3; ……………………. . } struct_var= {constant 1, constant 2, constant 3, …. }; © Oxford University Press 2014. All rights reserved.

Accessing the Members of a Structure • Each member of a structure can be used just like a normal variable, but its name will be a bit longer. • A structure member variable is generally accessed using a ‘. ’ (dot operator). • The syntax of accessing a member of a structure is: struct_var. member_name • For example, to assign value to the individual data members of the structure variable student 1, we may write: student 1. id = 01; © Oxford University Press 2014. All rights reserved.

Accessing the Members of a Structure • For example, struct student 1 = {01, "Rahul"}; studen 1. id = 2; struct student 2; • We can assign a structure to another structure of the same type. • Then to assign one structure variable to another, we will write: student 2 = student 1; © Oxford University Press 2014. All rights reserved.

More examples Declare and initialize struct point p 1 = {320, 100}; POINT p 2 = {200, 100}; Declare, set values, get values p 1. x = 320; p 1. y = 100; printf(“%d, %d”, p 1. x, p 1. y); p 2. x = 300; p 2. y = 300; printf(“%d, %d”, p 2. x, p 2. y); © Oxford University Press 2014. All rights reserved.

Nested Structures • A structure can be placed within another structure. • Such a structure that contains another structure as its member is called a nested structure. typedef struct { char first_name[20]; char mid_name[20]; char last_name[20]; } NAME; typedef struct { int dd; int mm; int yy; } DATE; typedef struct { int id; NAME name; DATE dob; } student; To assign values to the structure fields, we will write: student 1; Strcpy(&student 1. name. first_name , “Janak”); studdent 1. dob. dd = 15; studuent 1. dob. mm = 03; student 1. dob. yy= 1990; © Oxford University Press 2014. All rights reserved.

3. Pointers of Structures • C allows to create a pointer to a structure. • Like in other cases, a pointer to a structure is never itself a structure, but merely a variable that holds the address of a structure. • The syntax to declare a pointer to a structure can be given as: struct_name { data_type member_name 1; data_type member_name 2; . . } *ptr; OR struct_name *ptr; © Oxford University Press 2014. All rights reserved.

Access structure through pointers • For our student structure we can declare a pointer variable by writing: student *ptr, student 1; • The next step is to assign the address of stud to the pointer using the address operator (&). So to assign the address, we will write: ptr = &student 1; • To access the members of the structure, one way is to write: (*ptr). id; • An alternative to the above statement can be used by using ‘pointing-to’ operator (->) ptr->id = 1; © Oxford University Press 2014. All rights reserved.

Expression equivalence ptr -> id studuent 1. id (*ptr). id studuent 1. id &ptr->id &student 1. id &(*ptr). id &student 1. id © Oxford University Press 2014. All rights reserved.

POINT p 1; • The address of p 1 is &p 1, • The address of p 1. x is &p 1. x • Pointers POINT *pt; pt = &p 1; /* define a pointer of POINT */ /* assign the address of p 1 to pt */ pt -> x p 1. x (*pt). x p 1. x &pt->x &p 1. x &(*pt). x &p 1. x See demo example © Oxford University Press 2014. All rights reserved.

Self-referential Structures • Self-referential structures are those structures that contain a reference to data of its same type. • That is, a self-referential structure contains a pointer to a data that is of the same type as that of the structure. struct node { int val; struct node *next; }; • Here the structure node contains two types of data: an integer val and next that is a pointer to a node. You must be wondering why do we need such a structure? Actually, self-referential structure is the foundation of other data structures. © Oxford University Press 2014. All rights reserved.

3. Structure arrays • The general syntax for declaring an array of structure can be given as: struct_name struct_var[index]; e. g. student studuents[30]; • Now, to assign values to the ith student of the class, we will write: students[i]. id = 9; strcpy(&students[i]. name, “Peter”); © Oxford University Press 2014. All rights reserved.
![More examples POINT p10 declare an array of point types POINT pt More examples POINT p[10]; /* declare an array of point types */ POINT *pt;](https://slidetodoc.com/presentation_image_h/2cc89f801df123cc74d0f54235e1a58b/image-19.jpg)
More examples POINT p[10]; /* declare an array of point types */ POINT *pt; /* define a pointer variable for structure point */ pt = &p[0]; /* assign the address of p or p[0] to pt */ pt + 1 is pointing to p[1] (pt+1) -> x is equivalent to p[1]. x © Oxford University Press 2014. All rights reserved.

4. Pass Structure to Function • To pass any individual member of the structure to a function we must use the direct selection operator to refer to the individual members for the actual parameters. • The called program does not know if the two variables are ordinary variables or structure members. void display(int x, int y) { printf(“%d, %d”, x, y); } POINT p 1={2, 3}; display(p 1. x, p 1. y); //passing member values of p 1 to function display(int, int) © Oxford University Press 2014. All rights reserved.

Passing a Structure to a Function • When a structure is passed as an argument, it is passed using call by value method. That is a copy of each member of the structure is made. • The general syntax for passing a structure to a function and returning a structure can be given as: struct_name func_name(struct_name struct_var); Example void display(POINT p) { printf(“%d, %d”, p. x, p. y); } POINT p 1={2, 3}; display(p 1); / /passing entire structure p 1 to function display(POINT) © Oxford University Press 2014. All rights reserved.

Pass by Structure Reference • When a structure reference (pointer) is passed as an argument, the address of structure is passed to function • The general syntax for passing a structure to a function and returning a structure can be given as: struct_name func_name(struct_name *struct_var); void display(POINT *p) { printf(“%d, %d”, p->x, p->y); } POINT p 1={2, 3}; display(&p 1); / /passing the address of p 1 to function display(POINT*) See demo example © Oxford University Press 2014. All rights reserved.