C_lecture_5 - CS 11 C track: lecture 5 ¡ Last week:...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 11 C track: lecture 5 ¡ Last week: pointers ¡ This week: ¡ Pointer arithmetic ¡ Arrays and pointers ¡ Dynamic memory allocation ¡ The stack and the heap 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); /* deref j */ Pointer arithmetic (1) ¡ Can add/subtract integers to/from pointers int i[5] = { 1, 2, 3, 4, 5 }; int *j = i; /* (*j) == ? */ j++; /* (*j) == ? */ j += 2; /* (*j) == ? */ j -= 3; /* (*j) == ? */ Pointer arithmetic (2) ¡ 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! Pointer arithmetic (3) ¡ N.B. pointer arithmetic doesn't add/subtract address directly but in multiples of the size of the type in bytes int i = { 1, 2, 3, 4, 5 }; int *j = i; j++; /* means: j = j + sizeof(int);*/ Arrays and pointers (1) ¡ Arrays are pointers in disguise! ¡ Arrays: "syntactic sugar" for pointers int i[5] = {1, 2, 3, 4, 5}; printf("i[3] = %d\n", i[3]); printf("i[3] = %d\n", *(i + 3)); ¡ i[3] and *(i + 3) are identical! ¡ i is identical to &i[0] Arrays and pointers (2) ¡ Can use pointer arithmetic instead of array operations ¡ Can be faster! int array[1000]; for (i = 1; i < 998; i++) { array[i] = (array[i-1] + array[i] + array[i+1]) / 3.0; } Arrays and pointers (3) ¡ Exactly the same as: int array[1000]; for (i = 1; i < 998; i++) { *(array+i) = (*(array+i-1) + *(array+i) + *(array+i+1)) / 3.0; } Arrays and pointers (4) ¡ When you say *(array + i) , you have to add i to array and dereference ¡ but incrementing pointers by 1 is faster! Arrays and pointers (5) int array[1000]; int *p1, *p2, *p3; p1=array; p2=array+1; p3=array+2; for (i = 1; i < 998; i++) { *p2 = (*p1 + *p2 + *p3) / 3.0; p1++; p2++; p3++; } Arrays and pointers (6) ¡ We replaced 3 pointer additions with three pointer increments, which are usually faster ¡ Even more significant for 2-d arrays Dynamic memory allocation (1) ¡ Recall that we can't do this: int n = 10; int arr[ n ]; /* not legal C */ ¡ However, often want to allocate an array where size of array not known in advance ¡ This is known as "dynamic memory allocation" ¡ dynamic as opposed to "static" (size known at compile time) Dynamic memory allocation (2)...
View Full Document

Page1 / 48

C_lecture_5 - CS 11 C track: lecture 5 ¡ Last week:...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online