190 likes | 265 Views
EEE 243B Applied Computer Programming. Pointers and Arrays §10.1, 10.3 Prof Sylvain P. Leblanc. Review. ExchangeInts (INT16U* firstInt , INT16U* secondInt ); INT16U a = 8; INT16U b = 7; … ExchangeInts (? a ,?b );//what symbol replaces ? …
E N D
EEE 243BApplied Computer Programming Pointers and Arrays §10.1, 10.3 Prof Sylvain P. Leblanc
Review • ExchangeInts(INT16U* firstInt, INT16U* secondInt); • INT16U a = 8; • INT16U b = 7; • … • ExchangeInts(?a,?b);//what symbol replaces ? • … • ExchangeInts(INT16U* firstInt, INT16U* secondInt) • { • INT16U temp; • temp = ?firstInt; //what symbol replaces ? • ?firstInt = ?secondInt; • ?secondInt = temp; • return; • } Prof S.P. Leblanc
Outline • Variables and Addresses • Pointer Constants • Similarities between Arrays and Pointers Prof S.P. Leblanc
Introduction • 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 Prof S.P. Leblanc
1. 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 immutable Prof S.P. Leblanc
1. Variables and Addresses Symbolic Memory … void main() { … INT16U a = 145; INT16U* p = NULL; // a pointer … p = &a; //take a’s address and //store it in pointer p. } 12500 a 145 p 46798 Prof S.P. Leblanc
1. Variables and Addresses Symbolic Memory … void main() { … INT16U a = 145; INT16U* p = NULL; // a pointer … p = &a; //take a’s address and //store it in pointer p. } 12500 a 145 p 46798 These are constant Prof S.P. Leblanc
1. Variables and Addresses Symbolic Memory … void main() { … INT16U a = 145; INT16U* p = NULL; // a pointer … p = &a; //take a’s address and //store it in pointer p. } 12500 a 145 p 46798 These are variable Prof S.P. Leblanc
2. Pointer constants Memory Symbolic scores … void main() { … INT16U a = 145; INT16U* p = NULL; // a pointer INT16U 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 } 1 7 9 984 scores[2] a 12500 145 p 46798 Prof S.P. Leblanc
2. Pointer constants Memory Symbolic scores … void main() { … INT16U a = 145; INT16U* p = NULL; // a pointer INT16U 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 12500 145 p 46798 984 scores is a constant Prof S.P. Leblanc
2. 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 Prof S.P. Leblanc
2. 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]); • You can find the code for this example on the course website at pointerConstants.zip Memory Prof S.P. Leblanc
2. 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 • We see a few examples. Forouzan (Chapter 11) is full of other great examples!!! Prof S.P. Leblanc
Pointer constants Memory Symbolic a a[0] a[1] a[2] 1 7 9 … void main() { … INT16U a[3] = {1,7,9}; INT16U* p = a; // a pointer INT16U* r = &a[1]; … } 984 p r 46798 86753 984 986 Prof S.P. Leblanc
Pointer constants Memory Symbolic a a[0] a[1] a[2] … void main() { … INT16U a[3] = {1,7,9}; INT16U* p = a; // a pointer INT16U* r = &a[1]; … *p = 12; //This should be familiar } 12 7 9 984 p r 46798 86753 984 986 Prof S.P. Leblanc
Pointer constants Memory Symbolic a a[0] a[1] a[2] … void main() { … INT16U a[3] = {1,7,9}; INT16U* p = a; // a pointer INT16U* r = &a[1]; … *p = 12; //This should be familiar *a = 543; //Look! We dereference //an array! } 543 7 9 984 p r 46798 86753 984 986 Prof S.P. Leblanc
Pointer constants Memory Symbolic a a[0] a[1] a[2] … void main() { … INT16U a[3] = {1,7,9}; INT16U* p = a; // p points to a INT16U* r = &a[1]; … *p = 12; //This should be familiar *a = 543; //Look! We dereference //a table r[0] = 66; r[-1] = 77; r[1] = 111; } 77 66 111 984 p r 46798 86753 984 986 Prof S.P. Leblanc
Quiz Time Logical Symbolic a[0] a[1] a[2] a … void main() { INT16U a[3] = {1,7,9}; INT16U* p = NULL; INT16U* r = NULL; r = &a[1]; //draw the pointers p = &a[2]; //and fill the boxes *p = 8; //change value p[-2] = r[0]; //write values } 1 7 9 984 p r 46798 86753 Prof S.P. Leblanc
Next Class • Pointer Exercise • You will use the same environment for the Mid-Term Test and Final Exam!!!! Prof S.P. Leblanc