pointers - cs 2022: introduction to cpointers cs 2022: introduction to c instructor: hussam...

27
Pointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall 2011, Lecture 3

Upload: others

Post on 02-Sep-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

PointersCS 2022: Introduction to C

Instructor: Hussam Abu-Libdeh

(based on slides by Saikat Guha)

Fall 2011, Lecture 3

Pointers CS 2022, Fall 2011, Lecture 3

Page 2: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Administrivia

I Office Hours:Wednesdays 11:10am – 12:10pm4139 Upson Hall

I Announcements @http://twitter.com/cs2022

Pointers CS 2022, Fall 2011, Lecture 3

Page 3: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

PointerPointerA pointer is just another variable that points toanother variable. A pointer contains the memoryaddress of the variable it points to.

int i; // Integer

int *p; // Pointer to integer

int **m; // Pointer to int pointer

p = &i; // p now points to i

printf("%p", p); // address of i (in p)

m = &p; // m now points to p

printf("%p", m); // address of p (in m)

Pointers CS 2022, Fall 2011, Lecture 3

Page 4: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointers

Pointers CS 2022, Fall 2011, Lecture 3

Page 5: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointers

Pointers CS 2022, Fall 2011, Lecture 3

Page 6: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointers

Pointers CS 2022, Fall 2011, Lecture 3

Page 7: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointers

Pointers CS 2022, Fall 2011, Lecture 3

Page 8: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointers

Pointers CS 2022, Fall 2011, Lecture 3

Page 9: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointers

Pointers CS 2022, Fall 2011, Lecture 3

Page 10: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointers

Pointers CS 2022, Fall 2011, Lecture 3

Page 11: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointers

Pointers CS 2022, Fall 2011, Lecture 3

Page 12: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

swap1.c: Swap

#include <stdio.h>

int main() {

int a, b;

int *p, *q;

a = 10; b = 20;

p = &a; q = &b;

printf("Before: %d, %d, %d, %d",

a, b, *p, *q);

__ = __;

__ = __;

printf("After: %d, %d, %d, %d",

a, b, *p, *q);

return 0;

}

Before: 10, 20, 10, 20

After: 10, 20, 20, 10

Pointers CS 2022, Fall 2011, Lecture 3

Page 13: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

swap1.c: Swap

#include <stdio.h>

int main() {

int a, b;

int *p, *q;

a = 10; b = 20;

p = &a; q = &b;

printf("Before: %d, %d, %d, %d",

a, b, *p, *q);

p = &b;

q = &a;

printf("After: %d, %d, %d, %d",

a, b, *p, *q);

return 0;

}

Before: 10, 20, 10, 20

After: 10, 20, 20, 10

Pointers CS 2022, Fall 2011, Lecture 3

Page 14: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

swap2.c: Swap

#include <stdio.h>

int main() {

int a, b;

int *p, *q;

a = 10; b = 20;

p = &a; q = &b;

printf("Before: %d, %d, %d, %d",

a, b, *p, *q);

__ = __;

__ = __;

printf("After: %d, %d, %d, %d",

a, b, *p, *q);

return 0;

}

Before: 10, 20, 10, 20

After: 20, 10, 20, 10

Pointers CS 2022, Fall 2011, Lecture 3

Page 15: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

swap2.c: Swap

#include <stdio.h>

int main() {

int a, b;

int *p, *q;

a = 10; b = 20;

p = &a; q = &b;

printf("Before: %d, %d, %d, %d",

a, b, *p, *q);

a = 20;

b = 10;

printf("After: %d, %d, %d, %d",

a, b, *p, *q);

return 0;

}

Before: 10, 20, 10, 20

After: 20, 10, 20, 10

Pointers CS 2022, Fall 2011, Lecture 3

Page 16: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

swap3.c: Swap

#include <stdio.h>

int main() {

int a, b;

int *p, *q;

a = 10; b = 20;

p = &a; q = &b;

printf("Before: %d, %d, %d, %d",

a, b, *p, *q);

__ = __; __ = __;

__ = __; __ = __;

printf("After: %d, %d, %d, %d",

a, b, *p, *q);

return 0;

}

Before: 10, 20, 10, 20

After: 20, 10, 10, 20

Pointers CS 2022, Fall 2011, Lecture 3

Page 17: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

swap3.c: Swap

#include <stdio.h>

int main() {

int a, b;

int *p, *q;

a = 10; b = 20;

p = &a; q = &b;

printf("Before: %d, %d, %d, %d",

a, b, *p, *q);

a = 20; b = 10;

p = &b; q = &a;

printf("After: %d, %d, %d, %d",

a, b, *p, *q);

return 0;

}

Before: 10, 20, 10, 20

After: 20, 10, 10, 20

Pointers CS 2022, Fall 2011, Lecture 3

Page 18: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointers to Pointers!

#include <stdio.h>

int main() {

int a = 10, b = 20;

int *p = &a, *q = &b;

int **m = &p, **n = &q;

printf("X: %d %d %d %d %d %d\n",

**m, **n, *p, *q, a, b);

*m = *n; m = n;

*m = &a; n = &p;

**n = 30;

printf("Y: %d %d %d %d %d %d\n",

**m, **n, *p, *q, a, b);

return 0;

}

X:

Y:

Pointers CS 2022, Fall 2011, Lecture 3

Page 19: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointers to Pointers!

#include <stdio.h>

int main() {

int a = 10, b = 20;

int *p = &a, *q = &b;

int **m = &p, **n = &q;

printf("X: %d %d %d %d %d %d\n",

**m, **n, *p, *q, a, b);

*m = *n; m = n;

*m = &a; n = &p;

**n = 30;

printf("Y: %d %d %d %d %d %d\n",

**m, **n, *p, *q, a, b);

return 0;

}

X: 10 20 10 20 10 20

Y: 10 30 30 10 10 30

Pointers CS 2022, Fall 2011, Lecture 3

Page 20: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointer Arithmetic

Pointers CS 2022, Fall 2011, Lecture 3

Page 21: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointer Arithmetic

Pointers CS 2022, Fall 2011, Lecture 3

Page 22: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Memory in C

Variables

I Independent variables are a figment of yourimagination.

I When in C, think of memory cells. Eachmemory cell has an integer address.

I You can access any memory cell at any timefrom any function.

I Variable names are simply shortcuts for yourconvenience.

Pointers CS 2022, Fall 2011, Lecture 3

Page 23: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Nameless Variables

#include <stdlib.h>

int main() {

int *p = (int *)malloc(sizeof(int));

*p = 42;

return 0;

}

Pointers CS 2022, Fall 2011, Lecture 3

Page 24: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

A poor man’s array

int * newarray(int siz) {

return (int *)malloc(siz * sizeof(int));

}

void set(int *arr, int idx, int val) {

*(arr+idx) = val;

}

int get(int *arr, int idx) {

return *(arr + idx);

}

Pointers CS 2022, Fall 2011, Lecture 3

Page 25: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Multiple Return Values

void getab(int *a, int *b) {

*a = 10;

*b = 20;

}

int main() {

int a, b;

getab(&a, &b);

}

Pointers CS 2022, Fall 2011, Lecture 3

Page 26: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointers Recap

I int *ptr;

I Pointers are variables that store memoryaddresses of other variables

I Type of variable pointed to depends on type ofpointer:

I int *ptr points to an integer variableI char *ptr points to a character variableI Can cast between pointer types:

my_int_ptr = (int *) my_other_ptr;I void *ptr has an unspecified type; must be cast

to a type before used

Pointers CS 2022, Fall 2011, Lecture 3

Page 27: Pointers - CS 2022: Introduction to CPointers CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall

Pointers RecapI Two main operations:

I * dereference: gets the value at the memorylocation stored in a pointer

I & address of : gets the address of a variableI int *my_ptr = &my_var;

I Pointer arithmetic: directly manipulate apointer’s content to access other memorylocations

I Use with caution!: can crash your program dueto bad memory accesses

I However, it is useful in accessing and manipulatingdata structures

I Pointers to pointersI int **my_2d_array;

Pointers CS 2022, Fall 2011, Lecture 3