Variables Pointers and Arrays Professor Jennifer Rexford COS

Variables, Pointers, and Arrays Professor Jennifer Rexford COS 217 http: //www. cs. princeton. edu/courses/archive/spring 08/cos 217/ 1

Overview of Today’s Lecture • Pointers o Differences between value, variable, and pointer o Using pointers to do “call-by-reference” in C • Arrays o List of elements of the same type o Relationship between arrays and pointers o Example program to reverse an array • Strings o Array of characters ending in ‘ ’ • Struct o Multiple values grouped together o Dereferencing to access individual elements 2

Values, Variables, and Pointers • Value memory o E. g. , ‘M’ ‘s’ • Variable ‘M’ x o A named box that holds a value o E. g. , char x = ‘M’; ‘o’ • Pointer value o Address of the box o E. g. , &x • Pointer variable ‘o’ ‘t’ p o A box holding the address of the box o E. g. , char* p = &x; &x ‘h’ ‘y’ 3

Example Program #include <stdio. h> int main(void) { char x = ‘M’; char* p = &x; printf(“Value of x is %cn”, x); printf(“Address of x is %un”, p); printf(“Address of p is %un, ” &p); return 0; } • Output o Value of x is M o Address of x is 4290770463 o Address of p is 4290770456 What is this? *p 4

Values vs. Variables int n; n ? n = 217; n 217 n = n + 9; n 226 3 = n; ? ? &n a pointer value &3 ? ? What is this? *(&n) 5

Call by Value is Not Enough • Function parameters are transmitted by value o Values copied into “local variables” void swap(int x, int { int t; t = x; x = y; y = t; No! } int main(void) {. . . swap(a, b); . . . } y) x y a b 3 7 x y a b 7 3 3 7 6

Call by Reference Using Pointers • Use pointers to pass variables “by reference” void swap(int *x, int *y) { int t; t = *x; *x = *y; *y = t; } int main(void) {. . . swap(&a, &b); . . . } Yes x y a b 3 7 x y a b 7 3 7
![Arrays in C int a[5]; a 217 226 a is a value of type Arrays in C int a[5]; a 217 226 a is a value of type](http://slidetodoc.com/presentation_image/d933dfb3e66024664095c5bca1d1777b/image-8.jpg)
Arrays in C int a[5]; a 217 226 a is a value of type “pointer to int” What is “a” in the picture above? a is the pointer constant, not the five consecutive memory locations! 8
![Arrays and Pointers a int a[5]; p 217 int *p; 226 p = a; Arrays and Pointers a int a[5]; p 217 int *p; 226 p = a;](http://slidetodoc.com/presentation_image/d933dfb3e66024664095c5bca1d1777b/image-9.jpg)
Arrays and Pointers a int a[5]; p 217 int *p; 226 p = a; a is a value of type “pointer to int” (int *) p is a variable of type “pointer to int” (int *) OK: p = a; if (a == p). . . ; a[i] = p[j]; Wrong: a = p; 3 = i; 9
![C Does Not Do Bounds Checking! int a[5]; a 55 217 a[0] = 217; C Does Not Do Bounds Checking! int a[5]; a 55 217 a[0] = 217;](http://slidetodoc.com/presentation_image/d933dfb3e66024664095c5bca1d1777b/image-10.jpg)
C Does Not Do Bounds Checking! int a[5]; a 55 217 a[0] = 217; a[3] = 226; a[-1] = 55; a[7] = 320; 226 320 Unpleasant if you happened to have another variable before the array variable a, or after it! 10
![Arrays and Pointers int a[5]; a int *p, *q; p p = a; q Arrays and Pointers int a[5]; a int *p, *q; p p = a; q](http://slidetodoc.com/presentation_image/d933dfb3e66024664095c5bca1d1777b/image-11.jpg)
Arrays and Pointers int a[5]; a int *p, *q; p p = a; q p[1]= 44; 44 217 45 44 43 226 46 q = p + 2; q[-1] = 43; q[2] = 46; 11
![Pointer Arithmetic a int a[5]; 217 p 226 Subscript: a[i] “means” *(a+i) int *p; Pointer Arithmetic a int a[5]; 217 p 226 Subscript: a[i] “means” *(a+i) int *p;](http://slidetodoc.com/presentation_image/d933dfb3e66024664095c5bca1d1777b/image-12.jpg)
Pointer Arithmetic a int a[5]; 217 p 226 Subscript: a[i] “means” *(a+i) int *p; 4 bytes p = a + 2; Note: arithmetic scales by data size (e. g. , int of 4 bytes)12

Quaint usage of pointer arithmetic Add up the elements of an array: More straightforwardly: int a[100]; int sum, *p; int sum, i; . . . for (p=a; p<a+100; p++) for (i=0; i<100; i++) sum += *p; sum += a[i]; 13

Array Parameters to Functions void print. Array(int *p, int n) { int i; for (i=0; i<n; i++) printf(“%dn”, p[i]); } int fib[5] = {1, 1, 2, 3, 5}; int main(void) { print. Array(fib, 5); } 14

Array Params Pointer Params void print. Array(int *p, int n) {. . . } void print. Array(int p[5], int n) {. . . } void print. Array(int p[1000], int n) {. . . } All these declarations are equivalent! int main(void) { print. Array(fib, 5); } 15

Example Program: Reverse Array • Reverse the values in an array o Inputs: integer array a, and number of elements n o Output: values of a stored in reverse order • Algorithm o Swap the first and last elements in the array o Swap the second and second-to-last elements o … 77 31 94 5 186 16
![Example of Array by Reference void reverse (int a[], int n) { int l, Example of Array by Reference void reverse (int a[], int n) { int l,](http://slidetodoc.com/presentation_image/d933dfb3e66024664095c5bca1d1777b/image-17.jpg)
Example of Array by Reference void reverse (int a[], int n) { int l, r, temp; for (l=0, r=n-1; l<r; l++, r--) { temp = a[l]; a[l] = a[r]; a[r] = temp; } } int main(void) { reverse(fib, 5); } 17

Strings A string is just an array of characters (pointer to character), terminated by a ‘ ’ char (a null, ASCII code 0). char mystring[6] = {’H’, ’e’, ’l’, ’o’, ’ ’}; char mystring[6] = “Hello”; Equivalent char mystring[] = “Hello”; mystring H e l l o char *yourstring = “Hello”; yourstring • Different H e l l o 18
![Char Array and Pointer Manipulation char mystring[] = “Hello”; H e l l y Char Array and Pointer Manipulation char mystring[] = “Hello”; H e l l y](http://slidetodoc.com/presentation_image/d933dfb3e66024664095c5bca1d1777b/image-19.jpg)
Char Array and Pointer Manipulation char mystring[] = “Hello”; H e l l y o mystring J char *yourstring = “Hello”; yourstring • C e l l o H mystring[0] = ‘J’; yourstring[0] = ‘C’; yourstring = mystring; yourstring[4] = ‘y’; mystring = yourstring; 19