C_lecture_5

C_lecture_5 - CS 11 C track lecture 5 n n Last week pointers This week n n n n Pointer arithmetic Arrays and pointers Dynamic memory allocation The

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

View Full Document Right Arrow Icon
CS 11 C track: lecture 5 n Last week: pointers n This week: n Pointer arithmetic n Arrays and pointers n Dynamic memory allocation n 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 DocumentRight Arrow Icon
Pointers (from last week) n Address: location where data stored n 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) n 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 DocumentRight 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 DocumentRight 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 DocumentRight 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 DocumentRight 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 DocumentRight 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) n 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 N.B. sizeof is not a function n 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 DocumentRight Arrow Icon
Pointer arithmetic (12) n 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 DocumentRight Arrow Icon
Arrays and pointers (1) n Arrays are pointers in disguise! n 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)); n arr[3] and *(arr + 3) are identical! n arr is identical to &arr[0]
Background image of page 16
Arrays and pointers (2) n 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 DocumentRight Arrow Icon