1 / 17

EEE 243B Applied Computer Programming

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 ?

Download Presentation

EEE 243B Applied Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EEE 243BApplied Computer Programming Arrays and pointers

  2. 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

  3. Outline • Arrays and pointers • Variables and addresses • Pointer constants • Similarities between arrays and pointers Maj JGA Beaulieu & Capt MWP LeSauvage

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

More Related