350 likes | 434 Vues
Understand pointers, memory addresses, and how to use pointers for variables in computing. Learn how to handle pointers correctly and avoid common mistakes. Includes examples and easy steps to grasp the concept effectively.
E N D
Introduction to Computing Lecture 12Pointers • Dr. Bekir KARLIK • Yasar University • Department of Computer Engineering • bekir.karlik@yasar.edu.tr
Topics • Introduction to Pointers • Pointers and Function Parameters
ch: ’A’ 0x2000 Memory Address of a Variable char ch = ’A’; The memory address of the variable ch. The value of the variable ch.
char ch = ’A’; ’A’ 0x2000 &ch yields the value 0x2000 The &Operator • Gives the memory address of an object • Also known as the “address operator.”
“conversion specifier” for printing a memory address Example: char ch; printf(“%p”, &ch);
0x3A15 0x2000 chPtr Pointers A variable which can store the memory address of another variable. 0x1FFE 0x1FFF 0x2000 0x2001 0x2002 etc. ‘B’ ch
Pointers • A pointer is a variable. • Contains a memory address. • Points to a specific data type. • Pointer variables are usually named varPtr.
Example: char* cPtr; cPtr: 0x2004 Can store an address of variables of type char • We say cPtris a pointer to char.
c: cPtr: 0x2000 A 0x2004 Pointers and the & Operator Example: char c = ’A’; char *cPtr; cPtr = &c; Assigns the address ofc to cPtr. 0x2000
We can have pointers to any data type. • The * can be anywhere between the type and the variable. Example: int* numPtr; float* xPtr; Example: int *numPtr; float * xPtr; Notes on Pointers
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 %pconversion specifier. Example: printf(“%p”, numPtr); Notes on Pointers (cont.)
Notes on Pointers (cont.) Beware of pointers which are not initialized! int *numPtr; ??? numPtr
Notes on Pointers (cont.) • When declaring a pointer, it is a good idea to always initialize it toNULL (a special pointer constant). int *numPtr = NULL; NULL numPtr
The *Operator • Allows pointers to access to variables they point to. • Also known as “dereferencing operator.” • Should not be confused with the * in the pointer declaration.
cPtr: c: 0x2000 0x2004 A 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’; B 0x2000
Easy Steps to Pointers int num; char ch = ‘A’; float x; • Step 1: Declare the variable to be pointed to. num: ch: ‘A’ x:
Easy Steps to Pointers int num; char ch = ‘A’; float x; • Step 2: Declare the pointer variable. numPtr: int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; chPtr: xPtr: num: ch: ‘A’ x:
Easy Steps to Pointers int num; char ch = ‘A’; float x; • Step 3: Assign address of variable to pointer. numPtr: addr of num int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; chPtr: addr of ch xPtr: addr of x numPtr = # chPtr = &ch; xPtr = &x; num: ch: ‘A’ A pointer’s type has to correspond to the type of the variable it points to. x:
Easy Steps to Pointers int num; char ch = ‘A’; float x; • Step 4: De-reference the pointers. numPtr: addr of num int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; chPtr: addr of ch xPtr: addr of x numPtr = # chPtr = &ch; xPtr = &x; num: 65 *xPtr = 0.25; *numPtr = *chPtr; ch: ‘A’ x: 0.25
Pointers and Function Parameters • Example: Function to swap the values of two variables: swap x: 1 y: 2 x: 2 y: 1
#include <stdio.h> 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
#include <stdio.h> 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 x: 1 y: 2
#include <stdio.h> 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 tmp: a: 1 b: 2 x: 1 y: 2
#include <stdio.h> 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 tmp: 1 a: 1 b: 2 x: 1 y: 2
#include <stdio.h> 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 tmp: 1 a: 2 b: 2 x: 1 y: 2
#include <stdio.h> 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 tmp: 1 a: 2 b: 1 x: 1 y: 2
#include <stdio.h> 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 tmp: 1 a: 2 b: 1 x: 1 y: 2
#include <stdio.h> 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
#include <stdio.h> 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 x: 1 y: 2
#include <stdio.h> 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 tmp: a: addr of x b: addr of y x: 1 y: 2
#include <stdio.h> 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 tmp: 1 a: addr of x b: addr of y x: 1 y: 2
#include <stdio.h> 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 tmp: 1 a: addr of x b: addr of y x: 2 y: 2
#include <stdio.h> 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 tmp: 1 a: addr of x b: addr of y x: 2 y: 1
#include <stdio.h> 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 x: 2 y: 1
scanfdemystified. char ch; int numx; float numy; scanf(“%c %d %f”, &ch, &numx, &numy); Pointers and Function Arguments • Change the value of an actual parameter variable.