{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

C_lecture_5 - CS 11 C track lecture 5 n n Last week...

Info iconThis preview shows pages 1–25. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Pointers (from last week) Address: location where data stored Pointer: variable that holds an address int i = 10; int *j = &i; int k = 2 * (*j); /* dereference j */
Background image of page 2
Pointer arithmetic (1) Can add/subtract integers to/from pointers int arr[] = { 1, 2, 3, 4, 5 }; int *p = arr; /* (*p) == ? */ p++; /* (*p) == ? */ p += 2; /* (*p) == ? */ p -= 3; /* (*p) == ? */
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Pointer arithmetic (2) 1 2 4 3 5 arr p int arr[] = { 1, 2, 3, 4, 5 }; int *p = arr; /* (*p) == ? */
Background image of page 4
Pointer arithmetic (3) 1 2 4 3 5 arr p p++; /* (*p) == ? */
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Pointer arithmetic (4) 1 2 4 3 5 arr p p += 2; /* (*p) == ? */
Background image of page 6
Pointer arithmetic (5) 1 2 4 3 5 arr p p -= 3; /* (*p) == ? */
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Let's try that using addresses only... Pointer arithmetic (6)
Background image of page 8
Pointer arithmetic (7) arr p int arr[] = { 1, 2, 3, 4, 5 }; int *p = arr; /* (*p) == ? */ 1 2 4 3 5 0x1234 0x1234 0x1234
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Pointer arithmetic (8) arr p p++; /* (*p) == ? */ 1 2 4 3 5 0x1234 0x1234 0x1238 (assume 4 byte integers)
Background image of page 10
Pointer arithmetic (9) arr p p += 2; /* (*p) == ? */ 1 2 4 3 5 0x1234 0x1234 0x1240 (0x1240 = 0x1234 + 0x0c; 0x0c == 12 decimal or 3x4)
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Pointer arithmetic (10) arr p p-= 3; /* (*p) == ? */ 1 2 4 3 5 0x1234 0x1234 0x1234
Background image of page 12
Pointer arithmetic (11) Get size of a type using the sizeof operator: printf("size of integer: %d\n", sizeof (int)); printf("size of (int *): %d\n", sizeof (int *)); N.B. sizeof is not a function takes a type name as an argument!
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Pointer arithmetic (12) N.B. pointer arithmetic doesn't add/ subtract address directly but in multiples of the size of the type in bytes int arr[] = { 1, 2, 3, 4, 5 }; int *p = arr; p++; /* means: p = p + sizeof(int);*/
Background image of page 14
Pointer arithmetic (13) arr p p++; /* (*p) == ? */ 1 2 4 3 5 0x1234 0x1234 0x1238 ( j = 0x1234 + sizeof(int) = 0x1238 , not 0x1235 )
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Arrays and pointers (1) Arrays are pointers in disguise! Arrays: "syntactic sugar" for pointers int arr[] = {1, 2, 3, 4, 5}; printf("arr[3] = %d\n", arr[3]); printf("arr[3] = %d\n", *(arr + 3)); arr[3] and *(arr + 3) are identical! arr is identical to &arr[0]
Background image of page 16
Arrays and pointers (2) Can use pointer arithmetic wherever we use array operations; consider this: int i; double array[1000]; for (i = 1; i < 999; i++) { array[i] = (array[i-1] + array[i] + array[i+1]) / 3.0; }
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Arrays and pointers (3) Exactly the same as: int i; double array[1000]; for (i = 1; i < 999; i++) { *(array+i) = ( *(array+i-1) + *(array+i) + *(array+i+1) ) / 3.0; }
Background image of page 18
Arrays and pointers (4) When you say *(array + i) , you have to add i to array and dereference For large values of i , this is relatively slow Incrementing pointers by 1 is faster than adding a large number to a pointer Can use this fact to optimize the preceding code in an interesting way
Background image of page 19

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Arrays and pointers (5) double array[1000]; double *p1, *p2, *p3; p1=array; p2=array+1; p3=array+2; for (i = 1; i < 999; i++) { *p2 = (*p1 + *p2 + *p3) / 3.0; p1++; p2++; p3++; }
Background image of page 20
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}