This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full 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[i1] + 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+i1) + *(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 2d 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
This note was uploaded on 04/30/2008 for the course C CMPE 150 taught by Professor Tuna during the Spring '08 term at BoÄŸaziÃ§i University.
 Spring '08
 Tuna

Click to edit the document details