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 Administrivia I I Office Hours Wednesdays 11 10am 12 10pm 4139 Upson Hall Announcements http twitter com cs2022 Pointers CS 2022 Fall 2011 Lecture 3 Pointer Pointer A pointer is just another variable that points to another variable A pointer contains the memory address of the variable it points to int i int p int m Integer Pointer to integer Pointer to int pointer p i printf p p p now points to i address of i in p m p printf p m m now points to p address of p in m Pointers CS 2022 Fall 2011 Lecture 3 Pointers Pointers CS 2022 Fall 2011 Lecture 3 Pointers Pointers CS 2022 Fall 2011 Lecture 3 Pointers Pointers CS 2022 Fall 2011 Lecture 3 Pointers Pointers CS 2022 Fall 2011 Lecture 3 Pointers Pointers CS 2022 Fall 2011 Lecture 3 Pointers Pointers CS 2022 Fall 2011 Lecture 3 Pointers Pointers CS 2022 Fall 2011 Lecture 3 Pointers Pointers CS 2022 Fall 2011 Lecture 3 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 Before 10 20 10 20 After 10 20 20 10 printf After d d d d a b p q return 0 Pointers CS 2022 Fall 2011 Lecture 3 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 Before 10 20 10 20 After 10 20 20 10 p b q a printf After d d d d a b p q return 0 Pointers CS 2022 Fall 2011 Lecture 3 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 Before 10 20 10 20 After 20 10 20 10 printf After d d d d a b p q return 0 Pointers CS 2022 Fall 2011 Lecture 3 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 b Before 10 20 10 20 After 20 10 20 10 20 10 printf After d d d d a b p q return 0 Pointers CS 2022 Fall 2011 Lecture 3 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 Before 10 20 10 20 After 20 10 10 20 printf After d d d d a b p q return 0 Pointers CS 2022 Fall 2011 Lecture 3 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 p 20 b b q Before 10 20 10 20 After 20 10 10 20 10 a printf After d d d d a b p q return 0 Pointers CS 2022 Fall 2011 Lecture 3 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 X Y 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 Pointers CS 2022 Fall 2011 Lecture 3 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 X 10 20 10 20 10 20 Y 10 30 30 10 10 30 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 Pointers CS 2022 Fall 2011 Lecture 3 Pointer Arithmetic Pointers CS 2022 Fall 2011 Lecture 3 Pointer Arithmetic Pointers CS 2022 Fall 2011 Lecture 3 Memory in C Variables I I I I Independent variables are a figment of your imagination When in C think of memory cells Each memory cell has an integer address You can access any memory cell at any time from any function Variable names are simply shortcuts for your convenience Pointers CS 2022 Fall 2011 Lecture 3 Nameless Variables include stdlib h int main int p int malloc sizeof int p 42 return 0 Pointers CS 2022 Fall 2011 Lecture 3 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 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 Pointers Recap I I I int ptr Pointers are variables that store memory addresses of other variables Type of variable pointed to depends on type of pointer I I I I int ptr points to an integer variable char ptr points to a character variable Can cast between pointer types my int ptr int my other ptr void ptr has an unspecified type must be cast to a type before used Pointers CS 2022 Fall 2011 Lecture 3 Pointers Recap I Two main operations I I I I Pointer arithmetic directly manipulate a pointer s content to access other memory locations I I I dereference gets the value at the memory location stored in a pointer address of gets the address of a variable int my ptr my var Use with caution can crash your program due to bad memory accesses However it is useful in accessing and manipulating data structures Pointers to pointers I int my 2d array Pointers CS 2022 Fall 2011 Lecture 3
View Full Document
Unlocking...