1 / 28

240-222 Computer Programming Techniques Semester 1, 1998

240-222 Computer Programming Techniques Semester 1, 1998. 8. Introduction to Pointers. Objective of these slides: to introduce pointer variables (pointers). Overview. 1. Why Pointer Variables? 2. Boxes Again 3. What is a Pointer Variable? 4. Declaring a Pointer Variable

Download Presentation

240-222 Computer Programming Techniques Semester 1, 1998

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. 240-222 Computer Programming TechniquesSemester 1, 1998 8. Introduction to Pointers Objective of these slides: to introduce pointer variables (pointers)

  2. Overview 1. Why Pointer Variables? 2. Boxes Again 3. What is a Pointer Variable? 4. Declaring a Pointer Variable 5. Assignment to Pointers 6. Accessing a Pointer 7. Two Uses of * 8. More Examples 9. Coding Call by Reference

  3. 1. Why Pointer Variables? • most difficult part of C • the most useful ง can use them to code call by reference ง can use them to build lists, queues, stacks, trees, etc.

  4. 2. Boxes Again The box occurs at memory address 1232 (say): x int x; x 1232

  5. 3. What is a Pointer Variable? • A pointer variable can contain the memory address of another variable.

  6. 4. Declaring a Pointer Variable int *countptr; Read this declaration ‘backwards’:countptr can contain the address of an integer variable or (more usually):countptr can point to an integer variable

  7. 5. Assignment to Pointers • The operator for returning the address of a variable is &int x = 5; int *countptr; countptr = &x;

  8. Assigment in Pictures x 1232 (say) int x = 5; 5 countptr int *countptr; ? countptr = &x; countptr 1232 Usually, drawn as: x countptr 5

  9. Some Tricky Things int a;int *aptr;aptr = &a; is equivalent to: is equivalent to: But, int a, *aptr;aptr = &a; int a, *aptr = &a; int *aptr = &a, a; /* WRONG */

  10. Initialising a Pointer int *xptr; float *yptr; xptr = NULL; yptr = NULL;

  11. 6. Accessing a Pointer int c = 13;int *countptr; countptr = &c;printf("The value of countptr is %lu\n", countptr); 26634232 Result:

  12. printf("The value of the integer pointed to by countptr is %d\n", *countptr); Result: 13 • This is called dereferencing a pointer

  13. 7. Two Uses of * int *countptr; means countptrcan point to an integer variable All other occurrences of * mean dereference: printf("...", *countptr);

  14. 8. More Examples a gptr int a = 3; *gptr; 3 ? a gptr gptr = &a; 3 a gptr *gptr = 7; 7

  15. a gptr *gptr = *gptr + 2; 9 a gptr (*gptr)++; 10

  16. The & and * Operators Fig. 7.4 #include <stdio.h>int main(){ int a = 7; int *aptr; aptr = &a; /* continued on next slide */

  17. printf("Address of a is %lu\n", &a); printf("Value of aptr is %lu\n\n", aptr); printf("Value of a is %d\n", a); printf("Value of *aptr is %d\n\n", *aptr); printf("%s\n", "Proving that * and & are complements of each other."); printf("&*aptr = %lu\n", &*aptr); printf("*&aptr = %lu\n", *&aptr); return 0;}

  18. Address of a is 2042756Value of aptr is 2042756 Value of a is 7Value of *aptr is 7 Proving that * and & are complements of each other. &*aptr = 2042756*&aptr = 2042756

  19. Declarations and Initialisations int i = 3, j = 5, *p = &i, *q = &j, *r;double x; EquivalentExpression Expression Value p == &i p == (&i) 1 (true) p = i + 7 p = (i + 7) illegal **&p *p 3 r = &x r = (&x) illegal

  20. 9. Coding Call by Reference 9.1. Call by Value Reminder 9.2. Call by Reference Version 9.3. Swapping

  21. 9.1. Call by Value Reminder Fig. 7.6 /* increment using call by value */ #include <stdio.h> int add1(int);int main(){ int count = 7; printf("The original value of count is %d\n", count); count = add1(count); printf("The new value of count is %d\n", count); return 0;}

  22. int add1(int c){ return c++; /* increments local var c */} The original value of count is 7The new value of count is 8

  23. 9.2. Call By Reference Version Fig. 7.7 /* Incrementing a variable using call by reference coded with pointers */#include <stdio.h>void addp1(int *);int main(){ int count = 7; printf("The original value of count is %d\n", count); addp1(&count); printf("The new value of count is %d\n", count); return 0;}

  24. void addp1(int *countptr){ (*countptr)++; /* increments count in main() */} The original value of count is 7The new value of count is 8

  25. addp1() in Pictures int main(){ ... addp1(&count); ...} count 7 void addp1(int *countptr){ (*countptr)++;} countptr

  26. 9.3. Swapping Part of fig. 7.15 / 7.10. #include <stdio.h> void swap(int *, int *); int main(){ int a = 3, b = 7; printf("%d %d\n", a, b); /* 3 7 printed*/ swap( &a, &b); printf("%d %d\n", a, b); /* 7 3 printed*/ return 0;}

  27. void swap(int *p, int *q){ int tmp; tmp = *p; *p = *q; *q = tmp;}

  28. Swap() in Pictures int main(){ int a =3, b = 7; ... swap(&a, &b); ...} a b 3 7 void swap(int *p, int *q){ int tmp; tmp = *p; *p = *q; *q = tmp;} p q tmp

More Related