Computer Programming Lecture 2 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr
Topics Introduction to Pointers Pointers and Function Parameters
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)
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
Memory Address of a Variable char ch = A ; ch: 0x2000 A The memory address of the variable ch. The value of the variable ch.
The & Operator Gives the memory address of an object char ch = A ; 0x2000 A &ch yields the value 0x2000 Also known as the address operator.
Example: char ch; printf( %p, &ch); conversion specifier for printing a memory address
Pointers A variable which can store the memory address of another variable. 0x3A5 0x2000 chptr 0xFFE 0xFFF 0x2000 0x200 0x2002 B ch etc.
Pointers A variable directly references a value A pointer indirectly references a value count 7 countptr count 7
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;
Example: cptr: char* cptr; 0x2004 Can store an address of variables of type char We say cptr is a pointer to char.
Pointers and the & Operator Example: char c = A ; char *cptr; cptr = &c; Assigns the address of c to cptr. c: cptr: 0x2000 A 0x2004 0x2000
Notes on Pointers We can have pointers to any data type. Example: int* numptr; float* xptr; The * can be anywhere between the type and the variable. Example: int *numptr; float * xptr; char* chptr;
Notes on Pointers (cont.) You can assign the address of a variable to a compatible pointer using the & operator. Example: int anumber; int *numptr; numptr = &anumber;
Notes on Pointers (cont.) You can print the address stored in a pointer using the %p conversion specifier. Example: printf( %p, numptr);
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
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
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. c: cptr: 0x2000 B A 0x2004 0x2000
int rate; Pointers and the Operator int *p_rate; rate = 00; p_rate = &rate; printf( %d,rate); printf( %d,*p_rate); 00 00
Pointer Cautions int *numptr; Beware of pointers which are not initialized! *numptr = 2;??? numptr
Easy Steps to Pointers Step : Declare the variable to be pointed to. int num; char ch = A ; float x; num: ch: A x:
Easy Steps to Pointers Step 2: Declare the pointer variable. int num; char ch = A ; float x; numptr: NULL int* numptr = NULL; char *chptr = NULL; float * xptr = NULL; chptr: xptr: NULL NULL num: ch: A x:
Easy Steps to Pointers Step 3: Assign address of variable to pointer. int num; char ch = A ; float x; int* numptr = NULL; char *chptr = NULL; float * xptr = NULL; numptr = # chptr = &ch; xptr = &x; numptr: addr of num chptr: xptr: num: ch: addr of ch addr of x A A pointer s type has to correspond to the type of the variable it points to. x:
Easy Steps to Pointers Step 4: De-reference the pointers. int num; char ch = A ; float x; int* numptr = NULL; char *chptr = NULL; float * xptr = NULL; numptr = # chptr = &ch; xptr = &x; *xptr = 0.25; *numptr = *chptr; numptr: addr of num chptr: xptr: addr of ch addr of x num: 65 ch: A x: 0.25
Pointers and Function Parameters Example: Function to swap the values of two variables: x: y: 2 swap x: 2 y:
#include <stdio.h> Bad swap void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; main() int x =, y = 2; swap(x, y); printf( %d %d\n, x, y);
#include <stdio.h> Bad swap void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; main() int x =, y = 2; x: swap(x, y); printf( %d %d\n, x, y); y: 2
#include <stdio.h> void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; tmp: a: b: Bad swap 2 main() int x =, y = 2; x: swap(x, y); printf( %d %d\n, x, y); y: 2
#include <stdio.h> void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; tmp: a: b: Bad swap 2 main() int x =, y = 2; x: swap(x, y); printf( %d %d\n, x, y); y: 2
#include <stdio.h> void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; tmp: a: b: Bad swap 2 2 main() int x =, y = 2; x: swap(x, y); printf( %d %d\n, x, y); y: 2
#include <stdio.h> void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; tmp: a: b: Bad swap 2 main() int x =, y = 2; x: swap(x, y); printf( %d %d\n, x, y); y: 2
#include <stdio.h> void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; tmp: a: b: Bad swap 2 main() int x =, y = 2; x: swap(x, y); printf( %d %d\n, x, y); y: 2
#include <stdio.h> Good swap void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; main() int x =, y = 2; swap2(&x, &y); printf( %d %d\n, x, y);
#include <stdio.h> Good swap void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; main() int x =, y = 2; x: swap2(&x, &y); printf( %d %d\n, x, y); y: 2
#include <stdio.h> void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; tmp: a: b: Good swap addr of x addr of y main() int x =, y = 2; x: swap2(&x, &y); printf( %d %d\n, x, y); y: 2
#include <stdio.h> void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; tmp: a: b: Good swap addr of x addr of y main() int x =, y = 2; x: swap2(&x, &y); printf( %d %d\n, x, y); y: 2
#include <stdio.h> void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; tmp: a: b: Good swap addr of x addr of y main() int x =, y = 2; x: 2 swap2(&x, &y); printf( %d %d\n, x, y); y: 2
#include <stdio.h> void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; tmp: a: b: Good swap addr of x addr of y main() int x =, y = 2; x: 2 swap2(&x, &y); printf( %d %d\n, x, y); y:
#include <stdio.h> Good swap void swap2(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; main() int x =, y = 2; x: 2 swap2(&x, &y); printf( %d %d\n, x, y); y:
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);