Download presentation
Presentation is loading. Please wait.
Published bySiska Susanto Modified over 6 years ago
1
CSI-121 Structured Programming Language Lecture 16 Pointers
CSE1301 Sem CSI-121 Structured Programming Language Lecture 16 Pointers Lecture 16: Pointers
2
Topics Introduction to pointers Pointers and function parameters
3
’A’ Memory Address of a Variable ch: char ch = ’A’;
CSE1301 Sem Memory Address of a Variable char ch = ’A’; ’A’ 0x2000 ch: The memory address of the variable ch The value of the variable ch Lecture 16: Pointers
4
’A’ The & Operator &ch Gives the memory address of an object
CSE1301 Sem The & Operator Gives the memory address of an object char ch = ’A’; ’A’ 0x2000 &ch yields the value 0x2000 Also known as the “address operator” Lecture 16: Pointers
5
“conversion specifier” for printing a memory address
CSE1301 Sem Example: char ch; printf(“%p”, &ch); “conversion specifier” for printing a memory address Lecture 16: Pointers
6
Pointers A variable which can store the memory address of another variable 0x2000 chPtr 0x3A15 0x1FFE 0x1FFF 0x2000 0x2001 0x2002 etc ‘B’ ch
7
Pointers A pointer is a variable Contains a memory address
CSE1301 Sem Pointers A pointer is a variable Contains a memory address Points to a specific data type Pointer variables are usually named varPtr Lecture 16: Pointers
8
Can store an address of variables of type char
CSE1301 Sem Example: char* cPtr; cPtr: 0x2004 Can store an address of variables of type char We say cPtr is a pointer to char Lecture 16: Pointers
9
Pointers and the & Operator
CSE1301 Sem Pointers and the & Operator Example: char c = ’A’; char *cPtr; cPtr = &c; Assigns the address of c to cPtr A c: 0x2000 cPtr: 0x2004 0x2000 Lecture 16: Pointers
10
Notes on Pointers We can have pointers to any data type
CSE1301 Sem 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 Lecture 16: Pointers
11
Notes on Pointers (cont)
CSE1301 Sem Notes on Pointers (cont) You can assign the address of a variable to a “compatible” pointer using the & operator int aNumber; int *numPtr; numPtr = &aNumber; Example: You can print the address stored in a pointer using the %p conversion specifier printf(“%p”, numPtr); Example: Lecture 16: Pointers
12
The * Operator Allows pointers to access variables they point to
CSE1301 Sem 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 Lecture 16: Pointers
13
Pointers and the Operator
CSE1301 Sem Pointers and the Operator Example: char c = ’A’; char *cPtr = NULL; Changes the value of the variable which cPtr points to cPtr = &c; *cPtr = ’B’; cPtr: 0x2004 NULL A c: 0x2000 B 0x2000 Lecture 16: Pointers
14
Easy Steps to Pointers Step 1: Declare the variable to be pointed to
int num; char ch = ‘A’; float x; num: ch: ‘A’ x:
15
Easy Steps to Pointers (cont)
Step 2: Declare the pointer variable int num; char ch = ‘A’; float x; numPtr: NULL chPtr: NULL int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; xPtr: NULL num: ch: ‘A’ x:
16
Easy Steps to Pointers (cont)
Step 3: Assign address of variable to pointer int num; char ch = ‘A’; float x; numPtr: addr of num chPtr: addr of ch int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; xPtr: addr of x numPtr = # num: chPtr = &ch; xPtr = &x; ch: ‘A’ x: A pointer’s type has to correspond to the type of the variable it points to
17
Easy Steps to Pointers (cont)
Step 4: De-reference the pointers int num; char ch = ‘A’; float x; numPtr: addr of num chPtr: addr of ch int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; xPtr: addr of x numPtr = # chPtr = &ch; xPtr = &x; num: 65 ch: ‘A’ *xPtr = 0.25; *numPtr = *chPtr; x: 0.25
18
Notes on Pointers (cont)
CSE1301 Sem Notes on Pointers (cont) Beware of pointers which are not initialized! int *numPtr; ??? numPtr Lecture 16: Pointers
19
Notes on Pointers (cont)
CSE1301 Sem Notes on Pointers (cont) When declaring a pointer, it is a good idea to always initialize it to NULL (a special pointer constant) int *numPtr = NULL; NULL numPtr Lecture 16: Pointers
20
Pointers and Function Parameters
CSE1301 Sem Pointers and Function Parameters Example: Function to swap the values of two variables swap x: 1 y: 2 x: 2 y: 1 Lecture 16: Pointers
21
#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a;
CSE1301 Sem Bad swap #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; Lecture 16: Pointers
22
#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a;
CSE1301 Sem Bad swap #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; x: 1 y: 2 Lecture 16: Pointers
23
#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a;
CSE1301 Sem Bad swap #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; tmp: a: 1 b: 2 x: 1 y: 2 Lecture 16: Pointers
24
#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a;
CSE1301 Sem Bad swap #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 tmp: a: 1 b: 2 x: 1 y: 2 Lecture 16: Pointers
25
#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a;
CSE1301 Sem Bad swap #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; tmp: 1 a: 2 b: 2 x: 1 y: 2 Lecture 16: Pointers
26
#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a;
CSE1301 Sem Bad swap #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; tmp: 1 a: 2 b: 1 x: 1 y: 2 Lecture 16: Pointers
27
#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a;
CSE1301 Sem Bad swap #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; tmp: 1 a: 2 b: 1 x: 1 y: 2 Lecture 16: Pointers
28
#include <stdio.h> void swap2(int* a, int* b) { int tmp;
CSE1301 Sem Good swap #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; Lecture 16: Pointers
29
#include <stdio.h> void swap2(int* a, int* b) { int tmp;
CSE1301 Sem Good swap #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; x: 1 y: 2 Lecture 16: Pointers
30
#include <stdio.h> void swap2(int* a, int* b) { int tmp;
CSE1301 Sem Good swap #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; tmp: a: addr of x b: addr of y x: 1 y: 2 Lecture 16: Pointers
31
#include <stdio.h> void swap2(int* a, int* b) { int tmp;
CSE1301 Sem Good swap #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; tmp: 1 a: addr of x b: addr of y x: 1 y: 2 Lecture 16: Pointers
32
#include <stdio.h> void swap2(int* a, int* b) { int tmp;
CSE1301 Sem Good swap #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; tmp: 1 a: addr of x b: addr of y x: 2 y: 2 Lecture 16: Pointers
33
#include <stdio.h> void swap2(int* a, int* b) { int tmp;
CSE1301 Sem Good swap #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 tmp: a: addr of x b: addr of y x: 2 y: 1 Lecture 16: Pointers
34
#include <stdio.h> void swap2(int* a, int* b) { int tmp;
CSE1301 Sem Good swap #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; x: 2 y: 1 Lecture 16: Pointers
35
Pointers and Function Arguments
CSE1301 Sem 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); Lecture 16: Pointers
36
Reading King Chapter 11 Deitel and Deitel Chapter 7 ( )
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.