SlideShare uma empresa Scribd logo
1 de 20
Baixar para ler offline
Pointer
2
Pointer Declaration and Assignment
 Variables and address
– Structure of Memory
– Each variable is placed in a specified address
• Pointer: Address of variables
…
1000 ‘a’
1001
3.2
1005
4
…
ch, 1bytes
f, 4bytes
i, 4bytes
char ch = ‘a’ ;
float f = 3.2 ;
int i = 4 ;
address
3
Pointer Declaration and Assignment
 Variables and address
– We can get pointers of variables from the variables
char ch = ‘a’ ;
float f = 3.2 ;
int i = 4 ;
printf( “%d %d %dn”, &a, &f, &i ) ;
&(name of variable) == address of variable …
1000 ‘a’
1001
3.2
1005
4
…
Address of
operator
4
Pointer Declaration and Assignment
 Variables and address
– We can access the variable with the pointer of the variable
char ch ;
float f ;
int i ;
*(&ch) = ‘a’ ;
*(&f) = 3.2 ;
*(&i) = 4 ;
*(address) means the variable in the address
…
1000 ‘a’
1001
3.2
1005
4
…
char ch ;
float f ;
int i ;
ch = ‘a’ ;
f = 3.2 ;
i = 4 ;
Indirect
operator
5
Pointer Declaration and Assignment
 Pointer Variable
– The variable which can hold addresses of variables
char ch = ‘a’ ;
float f = 3.2 ;
int i = 4 ;
char *pch ;
float *pf ;
int *pi ;
pch = &ch ;
pf = &f ;
pi = &i ;
printf( “%d %d %dn”, pch, pf, pi ) ;
data_type * pointer_varaible;
Variable can hold
only ‘char’ type pointer
Variable can hold
only ‘float’ type pointer
Variable can hold
only ‘int’ type pointer
char *pch = &ch ;
float *pf = &f ;
int *pi = &i ;
6
Pointer Declaration and Assignment
 Pointer Variable
char ch ;
float f ;
int i ;
ch = ‘a’ ;
f = 3.2 ;
i = 4 ;
char ch ;
float f ;
int i ;
char *pch = &ch ;
float *pf = &f ;
int *pi = &i ;
*pch = ‘a’ ;
*pf = 3.2 ;
*pi = 4 ;
…
1000 ‘a’
1001
3.2
1005
4
…
7
Pointer Declaration and Assignment
 Pointer Variable
– The size of all pointer variables is 4 bytes (4 byte machine)
• why??
char *pch ;
float *pf ;
int *pi ;
printf( “%dn”, sizeof(pch) ) ;
printf( “%dn”, sizeof(pf) ) ;
printf( “%dn”, sizeof(pi) ) ;
int i = 0 ;
int *pi = & i;
printf( “%dn”, i ) ;
printf( “%dn”, *pi ) ;
printf( “%dn”, &pi ) ;
8
Pointer Declaration and Assignment
 Pointer Variable
– Since a pointer variable is also a variable, it has it own
address, like other variables do
int i = 0 ;
int *pi = &i;
printf( “%dn”, i ) ;
printf( “%dn”, *pi ) ;
printf( “%dn”, &pi ) ;
1000
…
1020
…
pi, 4 bytes
i, 4 bytes
 Example
9
Pointer Declaration and Assignment
[Ex] int *p;
int month=3;
p = &month;
Assign the address of ‘month’
to pointer variable
p month
3
p
1000
month
31000
Use an arrow instead of
writing address in a
pointer variable
 Example
10
Addressing and Dereferencing
[Ex] int a, b;
int *p;
a = b = 7;
p = &a;
printf(“*p = %dn”, *p);
*p = 3;
printf(“a = %dn”, a);
*p == 7
The variable pointed by p, that is a
The variable pointed by p, a, has 3
assign the address of a to p
*p = 7
a = 3
11
Addressing and Dereferencing
[Ex1] int a, b;
int *p;
a = b = 7;
p = &a;
*p = 3;
p = &b;
*p = 2 * *p – a;
pa
7
b
7
p
3
a b
7
pa
3 11
b
 Example
 Prone to error
12
Addressing and Dereferencing
[Ex1] int x, *p;
x = 10;
*p = x;
[Ex3] int x, *p;
x = 10;
p = x;
Error!!
Can’t assign value x to pointer p
because p is not initialized.
We don’t know where p points
Error!!
Can’t assign value x to pointer p
because p cannot hold an integer variable
13
Multi Pointer Variable
 Multi Pointer Variable
– i is a integer variable
– p is a pointer can hold the address of an integer variable
– q is a pointer can hold the address of a pointer variable of
integer variables
• The size of pointer variable q is 4 byte (32bit machine)
int i = 4 ;
int *p ;
int **q ;
 Example
14
Pointer Declaration and Assignment
[Ex] int i = 3;
int *p ;
int **q ;
p = &i ;
q = &p ;
p i
3
i
31000
p
10002000
q
20003000
q
 Example:
– What are the values of i and j,
respectively?
– Assume that the addresses of i,
j, p and q are 1000, 2000, 3000
and 4000, respectively.
– What are the values of p, q, r?
15
Pointer Declaration and Assignment
[Ex] int i = 3, j = 2;
int *p, *q ;
int **r ;
p = &i ;
q = &j ;
r = &p ;
*p = 4 ;
*q = 5 ;
**r = 6 ;
*r = &q ;
q =&i ;
**r = 7 ;
 Example
16
Swap Function
[Ex]
void swap(int i, int j) {
int temp = i ;
i = j;
j = temp;
}
int main(void) {
int a=3, b=7;
printf(“before swap : %d %dn”, a, b);
swap( a, b );
printf(“after swap : %d %dn”, a, b);
}
Can it swap values of a and b?
 Example
17
Swap Function
[Ex]
void swap(int *p, int *q) {
int temp = *p ;
*p = *q;
*q = temp;
}
int main(void) {
int a=3, b=7;
printf(“before swap : %d %dn”, a, b);
swap(&a, &b);
printf(“after swap : %d %dn”, a, b);
}
before swap : 3 7
after swap : 7 3
 Swap values of two pointer variables
18
Swap Function
[Ex]
void swap(int **pp, int **qq) {
int *temp = *pp ;
*pp = *qq;
*qq = temp;
}
int main(void) {
int a=3, b=7;
int *p = &a, *q = &b ;
printf(“before swap : %d %dn”, *p, *q);
swap(&p, &q);
printf(“after swap : %d %dn”, *p, *q);
}
before swap : 3 7
after swap : 7 3
19
Call-by-Value
1 1
a in main a in function
main() function function() function
a = a + 1 ;
When function(a); is
called, value of a in main
is copied to a in function
2
After Return;
1 Variable a in main and a in function are
not identical, so that value of a in main
is not changed
1
20
Call-by-Value
1 1
a in main a in function
main() function function() function
a = a + 1 ;
When function(a); is
called, value of a in main
is copied to a in function
2
After Return;
1 Variable a in main and a in function are
not identical, so that value of a in main
is not changed
1

Mais conteúdo relacionado

Mais procurados

C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd StudyChris Ohk
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointersMomenMostafa
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6Rumman Ansari
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
Function recap
Function recapFunction recap
Function recapalish sha
 
9 character string & string library
9  character string & string library9  character string & string library
9 character string & string libraryMomenMostafa
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c languagegourav kottawar
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programmingPrabhu Govind
 
C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2Rumman Ansari
 
6 c control statements branching & jumping
6 c control statements branching & jumping6 c control statements branching & jumping
6 c control statements branching & jumpingMomenMostafa
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th StudyChris Ohk
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 
Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13alish sha
 

Mais procurados (20)

C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd Study
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Function recap
Function recapFunction recap
Function recap
 
9 character string & string library
9  character string & string library9  character string & string library
9 character string & string library
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programming
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2
 
6 c control statements branching & jumping
6 c control statements branching & jumping6 c control statements branching & jumping
6 c control statements branching & jumping
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
CHAPTER 4
CHAPTER 4CHAPTER 4
CHAPTER 4
 
Lab 6
Lab 6Lab 6
Lab 6
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13
 

Destaque (7)

Advanced+pointers
Advanced+pointersAdvanced+pointers
Advanced+pointers
 
Data structure lecture 1
Data structure   lecture 1Data structure   lecture 1
Data structure lecture 1
 
C pointer
C pointerC pointer
C pointer
 
6 pointers functions
6 pointers functions6 pointers functions
6 pointers functions
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
C Pointers
C PointersC Pointers
C Pointers
 
Human values & professional ethics
Human values & professional ethicsHuman values & professional ethics
Human values & professional ethics
 

Semelhante a 9. pointer, pointer & function

Semelhante a 9. pointer, pointer & function (20)

iit c prog.ppt
iit c prog.pptiit c prog.ppt
iit c prog.ppt
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
Ch 7-pointers
Ch 7-pointersCh 7-pointers
Ch 7-pointers
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
Pointers
PointersPointers
Pointers
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointer basics.pptx
Pointer basics.pptxPointer basics.pptx
Pointer basics.pptx
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
C pointers
C pointersC pointers
C pointers
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
 
c program.ppt
c program.pptc program.ppt
c program.ppt
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 

Mais de 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 

Mais de 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 

Último (20)

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

9. pointer, pointer & function

  • 2. 2 Pointer Declaration and Assignment  Variables and address – Structure of Memory – Each variable is placed in a specified address • Pointer: Address of variables … 1000 ‘a’ 1001 3.2 1005 4 … ch, 1bytes f, 4bytes i, 4bytes char ch = ‘a’ ; float f = 3.2 ; int i = 4 ; address
  • 3. 3 Pointer Declaration and Assignment  Variables and address – We can get pointers of variables from the variables char ch = ‘a’ ; float f = 3.2 ; int i = 4 ; printf( “%d %d %dn”, &a, &f, &i ) ; &(name of variable) == address of variable … 1000 ‘a’ 1001 3.2 1005 4 … Address of operator
  • 4. 4 Pointer Declaration and Assignment  Variables and address – We can access the variable with the pointer of the variable char ch ; float f ; int i ; *(&ch) = ‘a’ ; *(&f) = 3.2 ; *(&i) = 4 ; *(address) means the variable in the address … 1000 ‘a’ 1001 3.2 1005 4 … char ch ; float f ; int i ; ch = ‘a’ ; f = 3.2 ; i = 4 ; Indirect operator
  • 5. 5 Pointer Declaration and Assignment  Pointer Variable – The variable which can hold addresses of variables char ch = ‘a’ ; float f = 3.2 ; int i = 4 ; char *pch ; float *pf ; int *pi ; pch = &ch ; pf = &f ; pi = &i ; printf( “%d %d %dn”, pch, pf, pi ) ; data_type * pointer_varaible; Variable can hold only ‘char’ type pointer Variable can hold only ‘float’ type pointer Variable can hold only ‘int’ type pointer char *pch = &ch ; float *pf = &f ; int *pi = &i ;
  • 6. 6 Pointer Declaration and Assignment  Pointer Variable char ch ; float f ; int i ; ch = ‘a’ ; f = 3.2 ; i = 4 ; char ch ; float f ; int i ; char *pch = &ch ; float *pf = &f ; int *pi = &i ; *pch = ‘a’ ; *pf = 3.2 ; *pi = 4 ; … 1000 ‘a’ 1001 3.2 1005 4 …
  • 7. 7 Pointer Declaration and Assignment  Pointer Variable – The size of all pointer variables is 4 bytes (4 byte machine) • why?? char *pch ; float *pf ; int *pi ; printf( “%dn”, sizeof(pch) ) ; printf( “%dn”, sizeof(pf) ) ; printf( “%dn”, sizeof(pi) ) ; int i = 0 ; int *pi = & i; printf( “%dn”, i ) ; printf( “%dn”, *pi ) ; printf( “%dn”, &pi ) ;
  • 8. 8 Pointer Declaration and Assignment  Pointer Variable – Since a pointer variable is also a variable, it has it own address, like other variables do int i = 0 ; int *pi = &i; printf( “%dn”, i ) ; printf( “%dn”, *pi ) ; printf( “%dn”, &pi ) ; 1000 … 1020 … pi, 4 bytes i, 4 bytes
  • 9.  Example 9 Pointer Declaration and Assignment [Ex] int *p; int month=3; p = &month; Assign the address of ‘month’ to pointer variable p month 3 p 1000 month 31000 Use an arrow instead of writing address in a pointer variable
  • 10.  Example 10 Addressing and Dereferencing [Ex] int a, b; int *p; a = b = 7; p = &a; printf(“*p = %dn”, *p); *p = 3; printf(“a = %dn”, a); *p == 7 The variable pointed by p, that is a The variable pointed by p, a, has 3 assign the address of a to p *p = 7 a = 3
  • 11. 11 Addressing and Dereferencing [Ex1] int a, b; int *p; a = b = 7; p = &a; *p = 3; p = &b; *p = 2 * *p – a; pa 7 b 7 p 3 a b 7 pa 3 11 b  Example
  • 12.  Prone to error 12 Addressing and Dereferencing [Ex1] int x, *p; x = 10; *p = x; [Ex3] int x, *p; x = 10; p = x; Error!! Can’t assign value x to pointer p because p is not initialized. We don’t know where p points Error!! Can’t assign value x to pointer p because p cannot hold an integer variable
  • 13. 13 Multi Pointer Variable  Multi Pointer Variable – i is a integer variable – p is a pointer can hold the address of an integer variable – q is a pointer can hold the address of a pointer variable of integer variables • The size of pointer variable q is 4 byte (32bit machine) int i = 4 ; int *p ; int **q ;
  • 14.  Example 14 Pointer Declaration and Assignment [Ex] int i = 3; int *p ; int **q ; p = &i ; q = &p ; p i 3 i 31000 p 10002000 q 20003000 q
  • 15.  Example: – What are the values of i and j, respectively? – Assume that the addresses of i, j, p and q are 1000, 2000, 3000 and 4000, respectively. – What are the values of p, q, r? 15 Pointer Declaration and Assignment [Ex] int i = 3, j = 2; int *p, *q ; int **r ; p = &i ; q = &j ; r = &p ; *p = 4 ; *q = 5 ; **r = 6 ; *r = &q ; q =&i ; **r = 7 ;
  • 16.  Example 16 Swap Function [Ex] void swap(int i, int j) { int temp = i ; i = j; j = temp; } int main(void) { int a=3, b=7; printf(“before swap : %d %dn”, a, b); swap( a, b ); printf(“after swap : %d %dn”, a, b); } Can it swap values of a and b?
  • 17.  Example 17 Swap Function [Ex] void swap(int *p, int *q) { int temp = *p ; *p = *q; *q = temp; } int main(void) { int a=3, b=7; printf(“before swap : %d %dn”, a, b); swap(&a, &b); printf(“after swap : %d %dn”, a, b); } before swap : 3 7 after swap : 7 3
  • 18.  Swap values of two pointer variables 18 Swap Function [Ex] void swap(int **pp, int **qq) { int *temp = *pp ; *pp = *qq; *qq = temp; } int main(void) { int a=3, b=7; int *p = &a, *q = &b ; printf(“before swap : %d %dn”, *p, *q); swap(&p, &q); printf(“after swap : %d %dn”, *p, *q); } before swap : 3 7 after swap : 7 3
  • 19. 19 Call-by-Value 1 1 a in main a in function main() function function() function a = a + 1 ; When function(a); is called, value of a in main is copied to a in function 2 After Return; 1 Variable a in main and a in function are not identical, so that value of a in main is not changed 1
  • 20. 20 Call-by-Value 1 1 a in main a in function main() function function() function a = a + 1 ; When function(a); is called, value of a in main is copied to a in function 2 After Return; 1 Variable a in main and a in function are not identical, so that value of a in main is not changed 1