170 likes | 233 Views
EEE 243B Applied Computer Programming. Arrays and pointers. Review. ExchangeInts(int *firstInt, int *secondInt); int a = 8; int b = 7; … ExchangeInts(?a,?b);//what symbol replaces ? … ExchangeInts(int *firstInt, int *secondInt) { int temp; temp = ?firstInt; //what symbol replaces ?
E N D
EEE 243BApplied Computer Programming Arrays and pointers
Review ExchangeInts(int *firstInt, int *secondInt); int a = 8; int b = 7; … ExchangeInts(?a,?b);//what symbol replaces ? … ExchangeInts(int *firstInt, int *secondInt) { int temp; temp = ?firstInt; //what symbol replaces ? ?firstInt = ?secondInt; ?secondInt = temp; return; } Maj JGA Beaulieu & Capt MWP LeSauvage
Outline • Arrays and pointers • Variables and addresses • Pointer constants • Similarities between arrays and pointers Maj JGA Beaulieu & Capt MWP LeSauvage
Arrays and pointers • Arrays and pointers have a very close relationship. • It is important to understand the similarities and the differences between pointers and arrays • Today we review some concepts that are important in understanding the relationship between arrays and pointers Maj JGA Beaulieu & Capt MWP LeSauvage
Variables and addresses • When we talk about variables, we must make a distinction between the value of a variable and its address • For a given run of the program, a variable will keep the same address but its value may change • Pointers are variables • The value of the pointer, which is an address may change during program execution • The address of the pointer variable however is constant Maj JGA Beaulieu & Capt MWP LeSauvage
Variables and addresses Symbolic Physical … void main() { … int a = 145; int *p = NULL; // a pointer … p = &a; //take a’s address and //store it in pointer p. } a 145 12500 p 12500 46798 Maj JGA Beaulieu & Capt MWP LeSauvage
Variables and addresses Symbolic Physical … void main() { … int a = 145; int *p = NULL; // a pointer … p = &a; //take a’s address and //store it in pointer p. } a 145 12500 p 12500 46798 These are constant Maj JGA Beaulieu & Capt MWP LeSauvage
Variables and addresses Symbolic Physical … void main() { … int a = 145; int *p = NULL; // a pointer … p = &a; //take a’s address and //store it in pointer p. } a 145 12500 p 12500 46798 These are variable Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer constants Symbolic Physical scores … void main() { … int a = 145; int *p = NULL; // a pointer int scores[3] = {1,7,9}; … p = &a; //take a’s address and //store it in pointer p. scores[0] = a;//Good p = scores; //Good, look beside scores = p; //Not legal } 145 7 9 984 scores[2] a 145 12500 p 984 This is a constant 46798 Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer constants • We remember that an array has three characteristics: a name, a fixed number of elements and a type • The name of the array is a constant; it is the address pointing to the first byte in the array. • The following slide may look bizarre but if you think of scores as a pointer constant it makes sense Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer constants • All of these statements print the same value: printf("Value of scores - a constant %d\n", scores); printf("Address of array scores %d\n",&scores); printf("Address of first element %d\n",&scores[0]); Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer constants • The similarities between arrays and pointers allow us to access the data in an array in two different ways: using an index or using a pointer • We can also use an index on a pointer to refer to elements in the array • We can even dereference the name of the array • And there is more!!! Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer constants Symbolic Physical a a[0] a[1] a[2] … void main() { … int a[3] = {1,7,9}; int *p = a; // a pointer int *r = &a[1]; … } 1 7 9 984 p r 984 986 46798 53215 Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer constants Symbolic Physical a a[0] a[1] a[2] … void main() { … int a[3] = {1,7,9}; int *p = a; // a pointer int *r = &a[1]; … *p = 12; //This should be familiar } 12 7 9 984 p r 984 986 46798 53215 Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer constants Symbolic Physical a a[0] a[1] a[2] … void main() { … int a[3] = {1,7,9}; int *p = a; // a pointer int *r = &a[1]; … *p = 12; //This should be familiar *a = 543; //Look! We dereference //an array } 543 7 9 984 p r 984 986 46798 53215 Maj JGA Beaulieu & Capt MWP LeSauvage
Pointer constants Symbolic Physical a a[0] a[1] a[2] … void main() { … int a[3] = {1,7,9}; int *p = a; // p points to a int *r = &a[1]; … *p = 12; //This should be familiar *a = 543; //Look! We dereference //an array r[0] = 66; r[-1] = 77; r[1] = 111; } 77 66 111 984 p r 984 986 46798 53215 Maj JGA Beaulieu & Capt MWP LeSauvage
Quiz Time Symbolic Physical a a[0] a[1] a[2] … void main() { … int a[3] = {1,7,9}; int *p = NULL; int *r = NULL; … r = &a[1]; //draw the pointers and p = &a[2]; //fill the boxes *p = 8; //change value p[-2] = r[0]; //write values } 1 7 9 984 p r 46798 53215 Maj JGA Beaulieu & Capt MWP LeSauvage