Upload
md-tanvir-hossain
View
86
Download
0
Embed Size (px)
Citation preview
WELCOME TO OURPRESENTATION
2
STRUCTURE AND POINTER
3
Prepared By:
Md. Tanvir HossainDepartment of CSEDaffodil International University
Jakir HossainDepartment of CSEDaffodil International University
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
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.
6
Defining a Structure :
struct structure_name{data_type
member_variable1;data_type
member_variable2;………………………………;data_type
member_variableN;};
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 :
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 :
9
Sample Output :
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
11
Passing the individual member to functions :
Structure members can be passed to functions as actual arguments in function call like ordinary variables.
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) { …………; }
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.
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
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
16
‘*’ , ‘&’ Pointer
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
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.
19
Operators used in Pointers
* &AddressDereferencing
(Address of)(Value of)
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’
21
Pointer Assignment
Int i = 1 , *ip ; //pointer declaration ip = &i ; //pointer assignment*ip = 3 ; //pointer assignment
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);}
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’
24
Arrays and pointers are closely related
Array name is like a constant pointer
Pointers can do array subscripting operations
25
Relation Between Structure And Pointer :
Structure and pointer are interrelated.We Avaiable Use Structure and pointer at together.Such as…….Linked listQueueStack
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
27
Overview..
Structure and pointer are the two important element of c programming. For solving many kind of problem we use this……………..
28
AnyQuestion?
29
Thank You !