280 likes | 404 Views
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
E N D
240-222 Computer Programming TechniquesSemester 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 5. Assignment to Pointers 6. Accessing a Pointer 7. Two Uses of * 8. More Examples 9. Coding Call by Reference
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.
2. Boxes Again The box occurs at memory address 1232 (say): x int x; x 1232
3. What is a Pointer Variable? • A pointer variable can contain the memory address of another variable.
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
5. Assignment to Pointers • The operator for returning the address of a variable is &int x = 5; int *countptr; countptr = &x;
Assigment in Pictures x 1232 (say) int x = 5; 5 countptr int *countptr; ? countptr = &x; countptr 1232 Usually, drawn as: x countptr 5
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 */
Initialising a Pointer int *xptr; float *yptr; xptr = NULL; yptr = NULL;
6. Accessing a Pointer int c = 13;int *countptr; countptr = &c;printf("The value of countptr is %lu\n", countptr); 26634232 Result:
printf("The value of the integer pointed to by countptr is %d\n", *countptr); Result: 13 • This is called dereferencing a pointer
7. Two Uses of * int *countptr; means countptrcan point to an integer variable All other occurrences of * mean dereference: printf("...", *countptr);
8. More Examples a gptr int a = 3; *gptr; 3 ? a gptr gptr = &a; 3 a gptr *gptr = 7; 7
a gptr *gptr = *gptr + 2; 9 a gptr (*gptr)++; 10
The & and * Operators Fig. 7.4 #include <stdio.h>int main(){ int a = 7; int *aptr; aptr = &a; /* continued on next slide */
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;}
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
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
9. Coding Call by Reference 9.1. Call by Value Reminder 9.2. Call by Reference Version 9.3. Swapping
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;}
int add1(int c){ return c++; /* increments local var c */} The original value of count is 7The new value of count is 8
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;}
void addp1(int *countptr){ (*countptr)++; /* increments count in main() */} The original value of count is 7The new value of count is 8
addp1() in Pictures int main(){ ... addp1(&count); ...} count 7 void addp1(int *countptr){ (*countptr)++;} countptr
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;}
void swap(int *p, int *q){ int tmp; tmp = *p; *p = *q; *q = tmp;}
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