1 CSE1301 Computer Programming Lecture 16 Pointers.
-
date post
20-Dec-2015 -
Category
Documents
-
view
238 -
download
2
Transcript of 1 CSE1301 Computer Programming Lecture 16 Pointers.
![Page 1: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/1.jpg)
1
CSE1301Computer Programming
Lecture 16Pointers
![Page 2: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/2.jpg)
2
Topics
• Introduction to pointers
• Pointers and function parameters
![Page 3: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/3.jpg)
3
char ch = ’A’;
’A’0x2000
ch:
Memory Address of a Variable
The value of the variable ch
The memory address of the
variable ch
![Page 4: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/4.jpg)
4
The & Operator• Gives the memory address of an object
• Also known as the “address operator”
&ch yields the value 0x2000
char ch = ’A’;
’A’0x2000
![Page 5: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/5.jpg)
5
“conversion specifier” for printing a memory address
char ch;
printf(“%p”, &ch);
Example:
![Page 6: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/6.jpg)
6
Pointers
ch
0x1FFF 0x2000 0x2001 0x20020x1FFEetc
‘B’
0x2000
chPtr
0x3A15
A variable which can store the memory address of another variable
![Page 7: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/7.jpg)
7
Pointers
• A pointer is a variable
• Contains a memory address
• Points to a specific data type• Pointer variables are usually named varPtr
![Page 8: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/8.jpg)
8
cPtr:
char* cPtr;Example:
• We say cPtr is a pointer to char
0x2004
Can store an address of variables of type char
![Page 9: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/9.jpg)
9
Pointers and the & Operator
Example:
A
c:
0x2000
char c = ’A’;
char *cPtr;
cPtr:
0x2004
cPtr = &c;
0x2000
Assigns the address of c to cPtr
![Page 10: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/10.jpg)
10
Notes on Pointers
int* numPtr;float* xPtr;
Example:
• We can have pointers to any data type
int *numPtr;float * xPtr;
Example:
• The * can be anywhere between the type and the variable
![Page 11: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/11.jpg)
11
Notes on Pointers (cont)
• You can print the address stored in a pointer using the %p conversion specifier
printf(“%p”, numPtr);Example:
• You can assign the address of a variable to a “compatible” pointer using the & operator
int aNumber;int *numPtr;
numPtr = &aNumber;
Example:
![Page 12: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/12.jpg)
12
Notes on Pointers (cont)
int *numPtr;Beware of pointers
which are not initialized!
???
numPtr
![Page 13: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/13.jpg)
13
Notes on Pointers (cont)
int *numPtr = NULL;
NULL
numPtr
• When declaring a pointer, it is a good idea to always initialize it to NULL (a special pointer constant)
![Page 14: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/14.jpg)
14
The * Operator
• Allows pointers to access variables they point to
• Also known as “dereferencing operator”
• Should not be confused with the * in the pointer declaration
![Page 15: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/15.jpg)
15
A
c:
0x2000
B
Pointers and the Operator
Example: char c = ’A’;
char *cPtr = NULL;
cPtr = &c;
*cPtr = ’B’;
Changes the value of the variable which cPtr
points to
cPtr:
0x2004
NULL0x2000
![Page 16: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/16.jpg)
16
Easy Steps to Pointers
• Step 1: Declare the variable to be pointed to
int num;char ch = ‘A’;float x;
num:
‘A’ch:
x:
![Page 17: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/17.jpg)
17
Easy Steps to Pointers (cont)
• Step 2: Declare the pointer variable
int* numPtr = NULL;char *chPtr = NULL;float * xPtr = NULL;
int num;char ch = ‘A’;float x;
numPtr:
chPtr:
xPtr:
num:
‘A’ch:
x:
NULL
NULL
NULL
![Page 18: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/18.jpg)
18
Easy Steps to Pointers (cont)
• Step 3: Assign address of variable to pointer
int* numPtr = NULL;char *chPtr = NULL;float * xPtr = NULL;
int num;char ch = ‘A’;float x;
numPtr = #
numPtr:
chPtr:
xPtr:
num:
‘A’ch:
x:A pointer’s type has to correspond to the type of the variable it points to
chPtr = &ch;xPtr = &x;
addr of x
addr of ch
addr of num
![Page 19: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/19.jpg)
19
Easy Steps to Pointers (cont)
• Step 4: De-reference the pointers
int* numPtr = NULL;char *chPtr = NULL;float * xPtr = NULL;
int num;char ch = ‘A’;float x;
numPtr = #chPtr = &ch;xPtr = &x;
*xPtr = 0.25;
num:
‘A’ch:
x:
numPtr: addr of num
addr of chchPtr:
addr of xxPtr:
65
0.25*numPtr = *chPtr;
![Page 20: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/20.jpg)
20
Pointers and Function Parameters
• Example: Function to swap the values of two variables
x: 1
y: 2swap
x: 2
y: 1
![Page 21: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/21.jpg)
21
#include <stdio.h>
void swap1(int a, int b){ int tmp;
tmp = a; a = b; b = tmp; return;}
int main(){ int x = 1, y = 2;
swap1(x, y); printf(“%d %d\n”, x, y); return 0;}
Bad swap
![Page 22: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/22.jpg)
22
#include <stdio.h>
void swap1(int a, int b){ int tmp;
tmp = a; a = b; b = tmp; return;}
int main(){ int x = 1, y = 2;
swap1(x, y); printf(“%d %d\n”, x, y); return 0;}
1
2
x:
y:
Bad swap
![Page 23: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/23.jpg)
23
#include <stdio.h>
void swap1(int a, int b){ int tmp;
tmp = a; a = b; b = tmp; return;}
int main(){ int x = 1, y = 2;
swap1(x, y); printf(“%d %d\n”, x, y); return 0;}
1
2
x:
y:
1
2
a:
b:
tmp:
Bad swap
![Page 24: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/24.jpg)
24
#include <stdio.h>
void swap1(int a, int b){ int tmp;
tmp = a; a = b; b = tmp; return;}
int main(){ int x = 1, y = 2;
swap1(x, y); printf(“%d %d\n”, x, y); return 0;}
1
2
x:
y:
1
2
a:
b:
1tmp:
Bad swap
![Page 25: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/25.jpg)
25
#include <stdio.h>
void swap1(int a, int b){ int tmp;
tmp = a; a = b; b = tmp; return;}
int main(){ int x = 1, y = 2;
swap1(x, y); printf(“%d %d\n”, x, y); return 0;}
1
2
x:
y:
2
2
a:
b:
1tmp:
Bad swap
![Page 26: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/26.jpg)
26
#include <stdio.h>
void swap1(int a, int b){ int tmp;
tmp = a; a = b; b = tmp; return;}
int main(){ int x = 1, y = 2;
swap1(x, y); printf(“%d %d\n”, x, y); return 0;}
1
2
x:
y:
2
1
a:
b:
1tmp:
Bad swap
![Page 27: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/27.jpg)
27
#include <stdio.h>
void swap1(int a, int b){ int tmp;
tmp = a; a = b; b = tmp; return;}
int main(){ int x = 1, y = 2;
swap1(x, y); printf(“%d %d\n”, x, y); return 0;}
1
2
x:
y:
2
1
a:
b:
1tmp:
Bad swap
![Page 28: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/28.jpg)
28
#include <stdio.h>
void swap2(int* a, int* b){ int tmp;
tmp = *a; *a = *b; *b = tmp; return;}
int main(){ int x = 1, y = 2;
swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}
Good swap
![Page 29: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/29.jpg)
29
#include <stdio.h>
void swap2(int* a, int* b){ int tmp;
tmp = *a; *a = *b; *b = tmp; return;}
int main(){ int x = 1, y = 2;
swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}
1
2
x:
y:
Good swap
![Page 30: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/30.jpg)
30
#include <stdio.h>
void swap2(int* a, int* b){ int tmp;
tmp = *a; *a = *b; *b = tmp; return;}
int main(){ int x = 1, y = 2;
swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}
1
2
x:
y:
addr of x
addr of y
a:
b:
tmp:
Good swap
![Page 31: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/31.jpg)
31
#include <stdio.h>
void swap2(int* a, int* b){ int tmp;
tmp = *a; *a = *b; *b = tmp; return;}
int main(){ int x = 1, y = 2;
swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}
1
2
x:
y:
addr of x
addr of y
a:
b:
1tmp:
Good swap
![Page 32: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/32.jpg)
32
#include <stdio.h>
void swap2(int* a, int* b){ int tmp;
tmp = *a; *a = *b; *b = tmp; return;}
int main(){ int x = 1, y = 2;
swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}
2
2
x:
y:
addr of x
addr of y
a:
b:
1tmp:
Good swap
![Page 33: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/33.jpg)
33
#include <stdio.h>
void swap2(int* a, int* b){ int tmp;
tmp = *a; *a = *b; *b = tmp; return;}
int main(){ int x = 1, y = 2;
swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}
2
1
x:
y:
addr of x
addr of y
a:
b:
1tmp:
Good swap
![Page 34: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/34.jpg)
34
#include <stdio.h>
void swap2(int* a, int* b){ int tmp;
tmp = *a; *a = *b; *b = tmp; return;}
int main(){ int x = 1, y = 2;
swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}
2
1
x:
y:
Good swap
![Page 35: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/35.jpg)
35
Pointers and Function Arguments
• Change the value of an actual parameter variable
• scanf demystified
char ch;
int numx;
float numy;
scanf(“%c %d %f”, &ch, &numx, &numy);
![Page 36: 1 CSE1301 Computer Programming Lecture 16 Pointers.](https://reader035.fdocuments.net/reader035/viewer/2022062421/56649d415503460f94a1b649/html5/thumbnails/36.jpg)
36
Reading
• King– Chapter 11
• Deitel and Deitel– Chapter 7 (7.1-7.4)