Pointers Revision Overview Revision of Pointers and structs

  • Slides: 38
Download presentation
Pointers (Revision)

Pointers (Revision)

Overview • • Revision of Pointers and structs Basic Pointer Arithmetic Pointers and Arrays

Overview • • Revision of Pointers and structs Basic Pointer Arithmetic Pointers and Arrays 2

Pointers • A pointer is a datatype. • You can create variables of this

Pointers • A pointer is a datatype. • You can create variables of this type as you would of other types. • Contains a memory address. • Points to a specific data type. int *pointer 1 = &x; addr of x 0 x 2000 int x; 1 3 0 x 9060

Pointer Operations Type* object; a. Ptr; • a. Ptr = &object assigns the address

Pointer Operations Type* object; a. Ptr; • a. Ptr = &object assigns the address of object to a. Ptr. • *a. Ptr allows access to the object through the pointer. • If a. Ptr points to a structure then (*a. Ptr). member is equivalent to a. Ptr-> member 4

Pointers and Function Arguments x: 1 y: 2 swap x: 2 y: 1 5

Pointers and Function Arguments x: 1 y: 2 swap x: 2 y: 1 5

#include <stdio. h> Solution 1 void fake. Swap(int a, int b) { int tmp;

#include <stdio. h> Solution 1 void fake. Swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; fake. Swap(x, y); printf(“%d %dn”, x, y); } 66

#include <stdio. h> Solution 1 void fake. Swap(int a, int b) { int tmp;

#include <stdio. h> Solution 1 void fake. Swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; fake. Swap(x, y); printf(“%d %dn”, x, y); } x: y: 1 0 x 2000 2 0 x 2010 77

#include <stdio. h> void fake. Swap(int a, int b) { int tmp; tmp =

#include <stdio. h> void fake. Swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; Solution 1 tmp: a: b: } int main() { int x = 1, y = 2; fake. Swap(x, y); printf(“%d %dn”, x, y); } x: y: 0 x 2060 1 0 x 2038 2 0 x 2040 1 0 x 2000 2 0 x 2010 88

#include <stdio. h> void fake. Swap(int a, int b) { int tmp; tmp =

#include <stdio. h> void fake. Swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; Solution 1 tmp: a: b: } int main() { int x = 1, y = 2; fake. Swap(x, y); printf(“%d %dn”, x, y); } x: y: 1 0 x 2060 1 0 x 2038 2 0 x 2040 1 0 x 2000 2 0 x 2010 99

#include <stdio. h> void fake. Swap(int a, int b) { int tmp; tmp =

#include <stdio. h> void fake. Swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; Solution 1 tmp: a: b: } int main() { int x = 1, y = 2; fake. Swap(x, y); printf(“%d %dn”, x, y); } x: y: 1 0 x 2060 2 0 x 2038 2 0 x 2040 1 0 x 2000 2 0 x 2010 10 10

#include <stdio. h> void fake. Swap(int a, int b) { int tmp; tmp =

#include <stdio. h> void fake. Swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; Solution 1 tmp: a: b: } int main() { int x = 1, y = 2; fake. Swap(x, y); printf(“%d %dn”, x, y); } x: y: 1 0 x 2060 2 0 x 2038 1 0 x 2040 1 0 x 2000 2 0 x 2010 11 11

#include <stdio. h> Solution 1 void fake. Swap(int a, int b) { int tmp;

#include <stdio. h> Solution 1 void fake. Swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; fake. Swap(x, y); printf(“%d %dn”, x, y); } x: y: 1 0 x 2000 2 0 x 2010 12 12

#include <stdio. h> Solution 2 void true. Swap(int* a, int* b) { int tmp;

#include <stdio. h> Solution 2 void true. Swap(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; true. Swap(&x, &y); printf(“%d %dn”, x, y); } 13 13

#include <stdio. h> Solution 2 void true. Swap(int* a, int* b) { int tmp;

#include <stdio. h> Solution 2 void true. Swap(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; true. Swap(&x, &y); printf(“%d %dn”, x, y); } x: y: 1 0 x 2000 2 0 x 2010 14 14

#include <stdio. h> void true. Swap(int* a, int* b) { int tmp; tmp =

#include <stdio. h> void true. Swap(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; Solution 2 tmp: a: b: } int main() { int x = 1, y = 2; true. Swap(&x, &y); printf(“%d %dn”, x, y); } x: y: 0 x 2060 addr of x 0 x 2038 addr of y 0 x 2040 1 0 x 2000 2 0 x 2010 15 15

#include <stdio. h> void true. Swap(int* a, int* b) { int tmp; tmp =

#include <stdio. h> void true. Swap(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; Solution 2 tmp: a: b: } int main() { int x = 1, y = 2; true. Swap(&x, &y); printf(“%d %dn”, x, y); } x: y: 1 0 x 2060 addr of x 0 x 2038 addr of y 0 x 2040 1 0 x 2000 2 0 x 2010 16 16

#include <stdio. h> void true. Swap(int* a, int* b) { int tmp; tmp =

#include <stdio. h> void true. Swap(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; Solution 2 tmp: a: b: } int main() { int x = 1, y = 2; true. Swap(&x, &y); printf(“%d %dn”, x, y); } x: y: 1 0 x 2060 addr of x 0 x 2038 addr of y 0 x 2040 2 0 x 2000 2 0 x 2010 17 17

#include <stdio. h> void true. Swap(int* a, int* b) { int tmp; tmp =

#include <stdio. h> void true. Swap(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; Solution 2 tmp: a: b: } int main() { int x = 1, y = 2; true. Swap(&x, &y); printf(“%d %dn”, x, y); } x: y: 1 0 x 2060 addr of x 0 x 2038 addr of y 0 x 2040 2 0 x 2000 1 0 x 2010 18 18

#include <stdio. h> Solution 2 void true. Swap(int* a, int* b) { int tmp;

#include <stdio. h> Solution 2 void true. Swap(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; true. Swap(&x, &y); printf(“%d %dn”, x, y); } x: y: 2 0 x 2000 1 0 x 2010 19 19

More on Pointers • You can print the address stored in a pointer using

More on Pointers • You can print the address stored in a pointer using the %p conversion specifier Example: printf(“%p”, num. Ptr); needs to know where to put the value - it needs the address of the variable as it takes pointers as parameters. scanf() Example: int i; scanf(“%d”, &i); 20

struct Data. Base. Rec { /* Very Large Structure */ }; typedef struct Data.

struct Data. Base. Rec { /* Very Large Structure */ }; typedef struct Data. Base. Rec Data. Base; 21

Data. Base read. New. Entry(Data. Base the. Data. Base) { /* Some code */

Data. Base read. New. Entry(Data. Base the. Data. Base) { /* Some code */ return the. Data. Base; } void print. Data. Base(Data. Base the. Data. Base) { /* Some more code */ } 22

void read. New. Entry(Data. Base* data. Base. Ptr) { /* Some code */ }

void read. New. Entry(Data. Base* data. Base. Ptr) { /* Some code */ } void print. Data. Base(const Data. Base* data. Base. Ptr) { /* Some more code */ } 23

void read. New. Entry(Data. Base* data. Base. Ptr) { /* Some code */ Address

void read. New. Entry(Data. Base* data. Base. Ptr) { /* Some code */ Address of Database } void print. Data. Base(const Database* Data. Base* data. Base. Ptr) { /* Some more code */ } 24

void read. New. Entry(Data. Base* data. Base. Ptr) { /* Some code */ }

void read. New. Entry(Data. Base* data. Base. Ptr) { /* Some code */ } Database cannot change in this function. void print. Data. Base(const Data. Base* data. Base. Ptr) { /* Some more code */ } 25

Why Pointers? • To modify a variable in a function that is not a

Why Pointers? • To modify a variable in a function that is not a global, or a local to that function • To save space • To save time • To use dynamic memory • Used extensively in linked structures 26

Pointers and Structures struct book. Rec{ float price; char name[7]; }; typedef struct book.

Pointers and Structures struct book. Rec{ float price; char name[7]; }; typedef struct book. Rec Book temp; scanf("%d %s", &temp. price, temp. name); 27

Pointers and Structures a. Ptr struct book. Rec{ float price; char name[7]; }; typedef

Pointers and Structures a. Ptr struct book. Rec{ float price; char name[7]; }; typedef struct book. Rec Book *a. Ptr; Book temp; a. Ptr = &temp; temp Why do you need the brackets? scanf("%d %s", &(a. Ptr->price), a. Ptr->name); 28

Pointers and Functions • To enable a function to access and change an object.

Pointers and Functions • To enable a function to access and change an object. • For large structures it is more efficient. • Use const specifier whenever a constant is intended. 29

Pointers and Arrays Type array[size]; Type* p. Ptr = array + i; Type* q.

Pointers and Arrays Type array[size]; Type* p. Ptr = array + i; Type* q. Ptr = array + j; • The name array is equivalent to &array[0] • p. Ptr++ increments p. Ptr to point to the next element of array. • p. Ptr += n increments p. Ptr to point to n elements beyond where it currently points. • p. Ptr-q. Ptr equals i-j. 30

Pointers and Arrays (cont) A normal 1 dimensional array: Type array[size]; • array[0] is

Pointers and Arrays (cont) A normal 1 dimensional array: Type array[size]; • array[0] is equivalent to *array • array[n] is equivalent to *(array + n) 31

Basic Pointer Arithmetic array: 0 x 2008 0 x 200 C 0 x 2010

Basic Pointer Arithmetic array: 0 x 2008 0 x 200 C 0 x 2010 0 x 2014 0 x 2018 0 x 2008 0 1 p. Ptr: 0 x 2004 float 2 3 4 q. Ptr: 0 x 2008 0 x 2000 NULL array[5]; float* p. Ptr = array; float* q. Ptr = NULL; 32

array: 0 x 2008 0 x 200 C 0 x 2010 0 x 2014

array: 0 x 2008 0 x 200 C 0 x 2010 0 x 2014 0 x 2018 0 x 2008 0 1 p. Ptr: 0 x 2004 float 2 3 4 q. Ptr: 0 x 200 C 0 x 2000 NULL array[5]; float* p. Ptr = array; float* q. Ptr = NULL; p. Ptr++; /* p. Ptr now holds the address: &array[1] */ 33

array: 0 x 2008 0 x 200 C 0 x 2010 0 x 2014

array: 0 x 2008 0 x 200 C 0 x 2010 0 x 2014 0 x 2018 0 x 2008 0 1 p. Ptr: 0 x 2004 float 2 3 4 q. Ptr: 0 x 2018 0 x 2000 NULL array[5]; float* p. Ptr = array; float* q. Ptr = NULL; p. Ptr++; /* p. Ptr = &array[1] */ p. Ptr += 3; /* p. Ptr now hold the address: &array[4] */ 34

array: 0 x 2008 0 x 200 C 0 x 2010 0 x 2014

array: 0 x 2008 0 x 200 C 0 x 2010 0 x 2014 0 x 2018 0 x 2008 0 1 p. Ptr: 0 x 2004 float 2 3 4 q. Ptr: 0 x 2018 0 x 2000 0 x 2010 array[5]; float* p. Ptr = array; float* q. Ptr = NULL; p. Ptr++; /* p. Ptr = &array[1] */ p. Ptr += 3; /* p. Ptr = &array[4] */ q. Ptr = array + 2; /*q. Ptr now holds the address &array[2]*/ 35

array: 0 x 2008 0 x 200 C 0 x 2010 0 x 2014

array: 0 x 2008 0 x 200 C 0 x 2010 0 x 2014 0 x 2018 0 x 2008 0 1 p. Ptr: 0 x 2004 float 2 3 4 q. Ptr: 0 x 2018 0 x 2000 0 x 2010 array[5]; float* p. Ptr = array; float* q. Ptr = NULL; p. Ptr++; /* p. Ptr = &array[1] */ p. Ptr += 3; /* p. Ptr = &array[4] */ q. Ptr = array + 2; /* q. Ptr = &array[2] */ printf(“%dn”, p. Ptr-q. Ptr); 36

char* strcpy(char* s, char* t) { int i = 0; while (t[i] != 0)

char* strcpy(char* s, char* t) { int i = 0; while (t[i] != 0) { s[i] = t[i]; i++; } s[i] = ''; return s; } char*strcpy(char* s, char* t) { char* p = s; while (*p != 0) { *p = *t; p++; t++; } return s; } 37 37

Next • Stacks – abstract data type – main operations – implementation 38

Next • Stacks – abstract data type – main operations – implementation 38