structure and pointer
TRANSCRIPT
![Page 1: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/1.jpg)
WELCOME TO OURPRESENTATION
![Page 2: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/2.jpg)
2
STRUCTURE AND POINTER
![Page 3: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/3.jpg)
3
Prepared By:
Md. Tanvir HossainDepartment of CSEDaffodil International University
Jakir HossainDepartment of CSEDaffodil International University
![Page 4: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/4.jpg)
What is structure?
Overview..
Structure With Function Arrays of structures What is a Pointer ? Why Pointers ?
Operators used in Pointers Relation Between
Structure And Pointer
STRUCTURE AND POINTER
![Page 5: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/5.jpg)
5
What is structure?
A structure is a collection of variables of different data types under a single name.
It is a collection of heterogeneous data.It can have integer, float, double or character
data in it.The variables are called members of the
structure.The structure is also called a user-defined data
type.
![Page 6: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/6.jpg)
6
Defining a Structure :
struct structure_name{data_type
member_variable1;data_type
member_variable2;………………………………;data_type
member_variableN;};
![Page 7: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/7.jpg)
7
struct student{char name[20];int roll_no;float marks;char gender;long int phone_no;};
• Multiple variables of struct student type can be declared as:
struct student st1, st2, st3;
Example :
![Page 8: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/8.jpg)
8
struct student{char name[20];int ID;float CSE_marks;char gender;long int phone_no;};
void main(){ struct student st1={"Ishtiaque",5482,13.5,'M',16021548}; struct student st2={“Rony,4288,15,'F',19845321};
printf ("Name: %s ID: %d CSE Marks: %.1f Gender: %c Phn: %d \n",st1.name,st1.ID,st1.CSE_marks,st1.gender,st1.phone_no);
printf ("Name: %s ID: %d CSE Marks: %.1f Gender: %c Phn: %d \n",st2.name,st2.ID,st2.CSE_marks,st2.gender,st2.phone_no);}
Example :
![Page 9: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/9.jpg)
9
Sample Output :
![Page 10: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/10.jpg)
10
Structure With Function :
We will consider four cases here:
Passing the individual member to functionsPassing whole structure to functionsPassing structure pointer to functionsPassing array of structure to functions
![Page 11: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/11.jpg)
11
Passing the individual member to functions :
Structure members can be passed to functions as actual arguments in function call like ordinary variables.
![Page 12: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/12.jpg)
12
Passing whole structure to functions :
Whole structure can be passed to a function by the syntax:
function _ name ( structure _ variable _ name );
The called function has the form: return _ type function _ name (struct tag _ name
structure _ variable _ name) { …………; }
![Page 13: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/13.jpg)
13
Passing structure pointer to functions :
In this case, address of structure variable is passed as an actual argument to a function.
The corresponding formal argument must be a structure type pointer variable.
![Page 14: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/14.jpg)
14
Arrays of structures• An ordinary array: One type of data
• An array of structs: Multiple types of data in each array element.
0 1 2 … 98 99
0 1 2 … 98 99
![Page 15: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/15.jpg)
15
Arrays of structures :• We often use arrays of structures.• Example:
StudentRecord Class[100];strcpy(Class[98].Name, “Bangladeshi man");Class[98].Id = 12345;strcpy(Class[98].Dept, "COMP");Class[98].gender = 'M';Class[0] = Class[98];
Bangladeshi man
12345 M
COMP
. . .
0 1 2 … 98 99
![Page 16: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/16.jpg)
16
‘*’ , ‘&’ Pointer
![Page 17: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/17.jpg)
17
In a generic sense, a “pointer” is anything that tells us where something can be found. Addresses in the phone book URLs for webpages Road signs
What is a Pointer ? A variable that holds a memory address.This address
is the location of another object in the memory
![Page 18: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/18.jpg)
18
Why Pointers ?They allow you to refer to large data structures in
a compact way
They facilitate sharing between different parts of programs
They make it possible to get new memory dynamically as your program is running
They make it easy to represent relationships among data items.
![Page 19: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/19.jpg)
19
Operators used in Pointers
* &AddressDereferencing
(Address of)(Value of)
![Page 20: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/20.jpg)
20
Int i=3;
Address of ‘i’ Value of ‘i’
X1000 x1004 x1008 x100c x1010 x1014
variablei
3(Value of i)
Address of i
‘&i’ ‘*i’
The value ‘3’ is saved in the memory location ‘x100c’
![Page 21: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/21.jpg)
21
Pointer Assignment
Int i = 1 , *ip ; //pointer declaration ip = &i ; //pointer assignment*ip = 3 ; //pointer assignment
![Page 22: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/22.jpg)
22
Lets Take an Example and See
how pointers
work
#include<stdio.h>void main(){int i=3;int *j;j=&i;printf(“i=%d”,i);printf(“j=%d”,j);}
![Page 23: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/23.jpg)
23X1000 x1004 x1008 x100c x1010 x1014
3Memory
variables Int iInt *j
Int i=3;Int *j;j = &i;
x100c
Create an integer variable ‘i’ and initialize it to 3
Create a pointer variable ‘j’- create value of ‘j’
Initialize the pointer value of ‘j’ to the address of ‘i’
![Page 24: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/24.jpg)
24
Arrays and pointers are closely related
Array name is like a constant pointer
Pointers can do array subscripting operations
![Page 25: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/25.jpg)
25
Relation Between Structure And Pointer :
Structure and pointer are interrelated.We Avaiable Use Structure and pointer at together.Such as…….Linked listQueueStack
![Page 26: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/26.jpg)
26
A structure's member can be accesssed through pointer in two ways:Accessing structure's member through pointer
Referencing pointer to another address to access memory
Using dynamic memory allocationReferencing pointer to
another address to access memory
Using dynamic memory allocation
![Page 27: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/27.jpg)
27
Overview..
Structure and pointer are the two important element of c programming. For solving many kind of problem we use this……………..
![Page 28: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/28.jpg)
28
AnyQuestion?
![Page 29: Structure And Pointer](https://reader038.vdocuments.net/reader038/viewer/2022103011/5878302f1a28abef5d8b6799/html5/thumbnails/29.jpg)
29
Thank You !