# 1 240-222 CPT: Pointers/8 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce pointer variables (pointers)

## Presentation on theme: "1 240-222 CPT: Pointers/8 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce pointer variables (pointers)"— Presentation transcript:

1 240-222 CPT: Pointers/8 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce pointer variables (pointers) 8. Introduction to Pointers

2 240-222 CPT: Pointers/8 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 240-222 CPT: Pointers/8 1. Why Pointer Variables? l most difficult part of C l the most useful ง can use them to code call by reference ง can use them to build lists, queues, stacks, trees, etc.

4 240-222 CPT: Pointers/8 2. Boxes Again The box occurs at memory address 1232 (say): int x; x x 1232

5 240-222 CPT: Pointers/8 3. What is a Pointer Variable? l A pointer variable can contain the memory address of another variable.

6 240-222 CPT: Pointers/8 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 240-222 CPT: Pointers/8 5. Assignment to Pointers The operator for returning the address of a variable is & int x = 5; int *countptr; countptr = &x;

8 240-222 CPT: Pointers/8 Assigment in Pictures int x = 5; x 5 1232 (say) int *countptr; countptr ? countptr = &x; countptr 1232 x 5 countptr Usually, drawn as:

9 240-222 CPT: Pointers/8 Some Tricky Things int a; int *aptr; aptr = &a; is equivalent to: But, int a, *aptr; aptr = &a; int a, *aptr = &a; int *aptr = &a, a;/* WRONG */

10 240-222 CPT: Pointers/8 Initialising a Pointer i nt *xptr; float *yptr; xptr = NULL; yptr = NULL;

11 240-222 CPT: Pointers/8 6. Accessing a Pointer int c = 13; int *countptr; countptr = &c; printf("The value of countptr is %lu\n", countptr); 2663423 2 Result :

12 240-222 CPT: Pointers/8 printf("The value of the integer pointed to by countptr is %d\n", *countptr); l This is called dereferencing a pointer Result: 13

240-222 CPT: Pointers/8 7. Two Uses of * int *countptr; means countptr can point to an integer variable All other occurrences of * mean dereference: printf("...", *countptr);

14 240-222 CPT: Pointers/8 8. More Examples agptr a a int a = 3; *gptr; gptr = &a; *gptr = 7; 3? 3 7

15 240-222 CPT: Pointers/8 agptr a *gptr = *gptr + 2; (*gptr)++; 9 10

16 240-222 CPT: Pointers/8 The & and * OperatorsFig. 7.4 #include int main() { int a = 7; int *aptr; aptr = &a; /* continued on next slide */

17 240-222 CPT: Pointers/8 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 240-222 CPT: Pointers/8 Address of a is 2042756 Value of aptr is 2042756 Value of a is 7 Value of *aptr is 7 Proving that * and & are complements of each other. &*aptr = 2042756 *&aptr = 2042756

19 240-222 CPT: Pointers/8 Declarations and Initialisations int i = 3, j = 5, *p = &i, *q = &j, *r; double x; Expression Equivalent Expression Value p == &ip == (&i)1 (true) p = i + 7p = (i + 7)illegal **&p*p3 r = &xr = (&x)illegal

20 240-222 CPT: Pointers/8 9. Coding Call by Reference 9.1.Call by Value Reminder 9.2.Call by Reference Version 9.3.Swapping

21 240-222 CPT: Pointers/8 9.1. Call by Value ReminderFig. 7.6 /* increment using call by value */ #include 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 240-222 CPT: Pointers/8 int add1(int c) { return c++; /* increments local var c */ } The original value of count is 7 The new value of count is 8

23 240-222 CPT: Pointers/8 9.2. Call By Reference VersionFig. 7.7 /* Incrementing a variable using call by reference coded with pointers */ #include 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 240-222 CPT: Pointers/8 void addp1(int *countptr) { (*countptr)++; /* increments count in main() */ } The original value of count is 7 The new value of count is 8

25 240-222 CPT: Pointers/8 addp1() in Pictures int main() {... addp1(&count);... } count7 void addp1(int *countptr) { (*countptr)++; } countptr

26 240-222 CPT: Pointers/8 9.3. SwappingPart of fig. 7.15 / 7.10. #include 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 240-222 CPT: Pointers/8 void swap(int *p, int *q) { int tmp; tmp = *p; *p = *q; *q = tmp; }

28 240-222 CPT: Pointers/8 Swap() in Pictures int main() { int a =3, b = 7;... swap(&a, &b);... } b 7 void swap(int *p, int *q) { int tmp; tmp = *p; *p = *q; *q = tmp; } a 3 qp tmp

Download ppt "1 240-222 CPT: Pointers/8 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce pointer variables (pointers)"

Similar presentations