Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 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

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

4 Dereference, example int x, y; int *pt; pt = &x; /* pt gets the address of x */ x = 3; /* 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 */

5 Type of Pointers int x; char c; float f; int* ptx; char *ptc; float *ptf; ptx = &x; /* OK */ ptx = &c; /* WRONG */ ptc = &c; /* OK */ ptf = &f; /* OK */ ptf = &x; /* WRONG */ Int pointer can hold address of int variable only. Char pointer can hold address of char variable only.

6 Pointer Example, Swap two values with pointer char c1 = 'A'; char c2 = 'Z'; char temp, *p; p = &c1; temp = *p; *p = c2; c2 = temp; c1 c2 temp p AZ 10 11 12 100 p 10 c1 c2 temp AZA ZZA ZAA

7 Pointers/Swap Addresses #include main() { char c = 'O', d= 'H'; char *p1, *p2, *temp; p1 = &c; p2 = &d; printf("%c%c", *p1, *p2); temp = p1; p1 = p2; p2 = temp; printf("%c%c", *p1, *p2); } The program prints: OHHO Swap the values of p1 and p2.

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

9 Passing Ordinary Variable v.s. Passing Pointer #include void func(int i, int *p); main() { int i = 1, j = 2; printf("%d %d\n", i, j); func(i,&j); printf("%d %d\n", i, j); } void func(int i, int *p) { ++i; *p = *p + 1; } prints: 1 2 1 3

10 Levels of Indirection main() { int x; /* a int value */ int *p; /* pointer to int */ int **pp; /* pointer to pointer to int */ x = 5; p = &x; pp = &p; printf("%d %d %d\n", x, *p, **pp); }

11 Levels of Indirection int x; /* int value */ int *p; /* pointer to int */ int **pp; /* pointer to pointer to int */ x = 5; p = &x; pp = &p; 5 Address 8 12 16 8 12 x p pp

12 Pointer to a pointer to a pointer … to char #include main() { char c='A'; char *p; char **p2; char ***p3; p = &c; p2 = &p; p3 = &p2; printf("%c%c%c%c\n", c, *p, **p2, ***p3); printf("%d\t%p\n", c, &c); printf("%p\t%p\n", p, &p); printf("%p\t%p\n", p2, &p2); printf("%p\t%p\n", p3, &p3); } AAAA 65 4c4a3 4c4a3 4c49c 4c49c 4c498 4c498 4c494 %p prints address in hexadecimal.

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


Download ppt "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."

Similar presentations


Ads by Google