Download presentation
Presentation is loading. Please wait.
Published byCarmel Harris Modified over 9 years ago
1
Computer Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr
2
Topics Introduction to Pointers Pointers and Function Parameters
3
Introduction Pointer is one of the most powerful features of C Pointers enable programs to simulate –call by reference –to create and manipulate dynamic data structures (that can grow and shrink)
4
Computer Memory Computer’s memory consists of many thousands of sequential storage locations, and each location is identified by a unique address –From 0 to maximum When you declare a variable in a C program, the compiler set aside a memory location with a unique address to store that variable –When your program uses the variable name, it automatically accesses the proper memory location
5
char ch = ’A’; ’A’ 0x2000 ch: Memory Address of a Variable The value of the variable ch. The memory address of the variable ch.
6
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
7
“conversion specifier” for printing a memory address char ch; printf(“%p”, &ch); Example:
8
Pointers ch 0x1FFF0x20000x20010x20020x1FFE etc. ‘B’ 0x2000 chPtr 0x3A15 A variable which can store the memory address of another variable.
9
Pointers A variable directly references a value A pointer indirectly references a value count 7 7 countPtr
10
Pointers A pointer is a variable Contains a memory address Points to a specific data type Pointer variables are usually named varPtr To declare a pointer, we use the following form typename *ptrname;
11
cPtr: char* cPtr; Example: We say cPtr is a pointer to char. 0x2004 Can store an address of variables of type char
12
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.
13
Notes on Pointers int* numPtr; float* xPtr; Example: We can have pointers to any data type. int *numPtr; float * xPtr; char* chPtr; Example: The * can be anywhere between the type and the variable.
14
Notes on Pointers (cont.) You can assign the address of a variable to a “compatible” pointer using the & operator. intaNumber; int*numPtr; numPtr = & aNumber; Example:
15
Notes on Pointers (cont.) You can print the address stored in a pointer using the %p conversion specifier. printf(“%p”, numPtr); Example:
16
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).
17
The * Operator Allows pointers to access to variables they point to Also known as “dereferencing or indirection operator” Should not be confused with the * in the pointer declaration or the multiplication operator Don't worry about the compiler's becoming confused
18
A c: 0x2000 B Pointers and the Operator Example: char c = ’A’; char *cPtr = NULL; cPtr: 0x2004 cPtr = &c; 0x2000 *cPtr = ’B’; Changes the value of the variable which cPtr points to.
19
Pointers and the Operator int rate; int *p_rate; rate = 100; p_rate = &rate; printf(“%d”,rate); printf(“%d”,*p_rate); 100
20
Pointer Cautions int *numPtr; *numPtr = 12; Beware of pointers which are not initialized! ??? numPtr
21
Easy Steps to Pointers Step 1: Declare the variable to be pointed to. intnum; charch = ‘A’; float x; num: ‘A’ ch: x:
22
Easy Steps to Pointers Step 2: Declare the pointer variable. int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; intnum; charch = ‘A’; float x; numPtr: NULL chPtr: NULL xPtr: num: ‘A’ ch: x:
23
Easy Steps to Pointers Step 3: Assign address of variable to pointer. int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; intnum; charch = ‘A’; float x; numPtr = # chPtr = &ch; xPtr = &x; numPtr: addr of num addr of ch chPtr: addr of x xPtr: num: ‘A’ ch: x: A pointer’s type has to correspond to the type of the variable it points to.
24
Easy Steps to Pointers Step 4: De-reference the pointers. int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; intnum; charch = ‘A’; float x; numPtr = # chPtr = &ch; xPtr = &x; *xPtr = 0.25; *numPtr = *chPtr; num: 65 ‘A’ ch: 0.25 x: numPtr: addr of num addr of ch chPtr: addr of x xPtr:
25
Pointers and Function Parameters Example: Function to swap the values of two variables: x: 1 y: 2 swap x: 2 y: 1
26
#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } Bad swap
27
#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: Bad swap
28
#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 1 2 a: b: tmp: Bad swap
29
#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 1 2 a: b: 1 tmp: Bad swap
30
#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 2 2 a: b: 1 tmp: Bad swap
31
#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 2 1 a: b: 1 tmp: Bad swap
32
#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 2 1 a: b: 1 tmp: Bad swap
33
#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } Good swap
34
#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } 1 2 x: y: Good swap
35
#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } 1 2 x: y: addr of x addr of y a: b: tmp: Good swap
36
#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } 1 2 x: y: addr of x addr of y a: b: 1 tmp: Good swap
37
#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } 2 2 x: y: addr of x addr of y a: b: 1 tmp: Good swap
38
#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } 2 1 x: y: addr of x addr of y a: b: 1 tmp: Good swap
39
#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } 2 1 x: y: Good swap
40
Pointers and Function Arguments Change the value of an actual parameter variable. scanf demystified. char ch; intnumx; float numy; scanf(“%c %d %f”, &ch, &numx, &numy);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.