Pointer Variables The normal variables hold values For
Pointer Variables • The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value 2. • Pointer variables hold addresses: specific locations in computer's memory. Pointers also have different types: int *p; declare a pointer p of type int. The variable p can hold addresses of ints.
Address Operator & • & when applied to a variable produces the address of that variable. int x; int *pt; pt = &x; • The first line defines the variable x. It is in a specific location in memory, say at 9640 (byte position). • The second line defines a pointer variable of type int. • The last line assigns pt to hold the value of the address of x. I. e. , pt contains the value 9640. We say pt points to x. x pt 9640
Dereference * • Given that int x; int *pt; pt = &x; when pt appears in an expression, it gives you the value of pt. That is the address of variable x (say 9640). • When *pt appears in an expression, it gives the value of the variable pt is pointing to. That is, the value of x (if pt has the address of x). This is called dereference.
Dereference, example int x, y; int *pt; pt = &x; x = 3; /* pt gets the address of x */ /* x gets 3 */ y = *pt; /* y gets the value that the pointer pt points to. Since pt has the address of x, y get the value of x. That is, y gets 3 */ *pt = 4; /* The variable at the address that pt is pointing to gets the value 4. That is, x reassigns to the value 4 */
Type of Pointers int x; char c; float f; int* ptx; char *ptc; float *ptf; ptx ptc ptf = = = &x; &c; &f; &x; /* /* /* OK */ WRONG */ Int pointer can hold address of int variable only. Char pointer can hold address of char variable only.
Pointer Example, Swap two values with pointer char c 1 = 'A'; char c 2 = 'Z'; char temp, *p; c 1 c 2 temp A Z 10 11 12 p = &c 1; p 100 p 10 temp = *p; c 1 c 2 temp A Z A *p = c 2; c 2 = temp; c 1 c 2 temp Z Z A A
Pointers/Swap Addresses #include <stdio. h> #include <stdlib. h> main() { char c = 'O', d= 'H'; char *p 1, *p 2, *temp; p 1 = &c; p 2 = &d; printf("%c%c", *p 1, *p 2); temp = p 1; Swap the values p 1 = p 2; of p 1 and p 2 = temp; printf("%c%c", *p 1, *p 2); } The program prints: OHHO
Initializing a Pointer int i; int *p = &i; • The declarations allocate storage for two cells, int i and pointer to int p. It also initializes p to have the address of i. • Variable i must be declared before p in order to use it in the expression &i.
Passing Ordinary Variable v. s. Passing Pointer #include <stdio. h> void func(int i, int *p); main() { int i = 1, j = 2; printf("%d %dn", i, j); func(i, &j); printf("%d %dn", i, j); } void func(int i, int *p) { ++i; *p = *p + 1; } prints: 1 2 1 3
Levels of Indirection <stdio. h> main() { int x; int *p; int **pp; /* a int value */ /* pointer to int */ x = 5; p = &x; pp = &p; printf("%d %d %dn", x, *p, **pp); }
Levels of Indirection int x; int *p; int **pp; /* int value */ /* pointer to int */ x = 5; p = &x; pp = &p; x p pp 5 8 12 Address 8 12 16
Pointer to a pointer … to char #include <stdio. h> main() { char c='A'; char *p; char **p 2; char ***p 3; p = &c; p 2 = &p; p 3 = &p 2; printf("%c%cn", c, *p, **p 2, ***p 3); printf("%dt%pn", c, &c); printf("%pt%pn", p, &p); printf("%pt%pn", p 2, &p 2); printf("%pt%pn", p 3, &p 3); } AAAA 65 4 c 4 a 3 4 c 49 c 4 c 498 4 c 494 %p prints address in hexadecimal.
Reading/Home Working • Read Chapter 7, page 306 to 319. • Work on Problems – Section 7. 1, page 315, exercise 1, 3, 5. – Section 7. 2, page 320, exercise 1, 3, 5, 7. • Check your answers in the back of the textbook. Do not hand in.
- Slides: 13