C Pointers Systems Programming Pointers Pointers and Addresses

  • Slides: 20
Download presentation
C Pointers Systems Programming: Pointers

C Pointers Systems Programming: Pointers

Pointers and Addresses § Pointers § Using Pointers in Call by Reference § Swap

Pointers and Addresses § Pointers § Using Pointers in Call by Reference § Swap – A Pointer Example § Pointers and Arrays § Operator Precedence Example § Systems Programming: 2

Variables § Variable names correspond to memory locations in memory. Every variable has a

Variables § Variable names correspond to memory locations in memory. Every variable has a type, a name and a value. int i; i i = 4; 4 32212242 (the address of i ) &i Systems Programming: Pointers 3

Print an Address int main () { int i; i = 4; printf(‘i =

Print an Address int main () { int i; i = 4; printf(‘i = %d, address of i = %un”, i, &i); return 0; } $. /ptr 1 i = 4, address of i = 3220392980 Systems Programming: Pointers 4

Pointers § What is a pointer? – a variable that contains a memory address

Pointers § What is a pointer? – a variable that contains a memory address as its value. – Pointers contain the address of a variable that has a specific value (an indirect reference). § Pointers in C are typed – a pointer to a variable of type int – a pointer to a variable of type char – a pointer to a defined type or an object. 2007 Pearson Ed -All rights reserved. Systems Programming: Pointers 5

Fig. 7. 1 Directly and indirectly referencing a variable 2007 Pearson Ed -All rights

Fig. 7. 1 Directly and indirectly referencing a variable 2007 Pearson Ed -All rights reserved. Systems Programming: Pointers 6

Pointers /* Welcome to the world of Pointers! Pointers are a powerful tool */

Pointers /* Welcome to the world of Pointers! Pointers are a powerful tool */ int main () { int i; int *ptr; /* pointer declaration */ } i = 4; ptr = &i; printf(" i = %dn address of i = %un address of pointer = %un", i, ptr, &ptr); return 0; . /ptr 2 i=4 address of i = 3219352564 address of pointer = 3219352560 Systems Programming: Pointers 7

Pointers /* Do you think in Hex ? */ int main () { int

Pointers /* Do you think in Hex ? */ int main () { int i; int *ptr; ptr bfe 07240 bfe 07244 4 i i = 4; ptr = &i; printf(" i = %dn address of i = %pn address of pointer = %pn", i, ptr, &ptr); return 0; } . /ptr 3 i=4 address of i = 0 xbfe 07244 address of pointer = 0 xbfe 07240 Systems Programming: Pointers 8

Pointers /* Never trust a Compiler. */ int j, i; /* think globally! */

Pointers /* Never trust a Compiler. */ int j, i; /* think globally! */ 8049654 ptr 1 int *ptr 1, *ptr 2; void printit () ptr 2 804964 c { printf(" i = %2 d, ptr 1 = %pn", i, ptr 1); printf(" j = %2 d, ptr 2 = %pn", j, ptr 2); } 19 8 int main () 9 j { i i = 4; j = 8; 4 6 ptr 1 = &i; ptr 2 = &j; printit (); . /ptr 4 *ptr 2 = *ptr 2 + 1; i =this 4, */ptr 1 = 0 x 8049654 ptr 1 = ptr 1 - 2; /* You cannot know printit (); j = 8, ptr 2 = 0 x 804964 c i = 6; i = 4, ptr 1 = 0 x 804964 c *ptr 1 = *ptr 1 + 10; printit (); j = 9, ptr 2 = 0 x 804964 c return 0; i = 6, ptr 1 = 0 x 804964 c } j = 19, Pointers ptr 2 Systems Programming: = 0 x 804964 c 9

7. 4 Calling Functions by Reference § § Call by reference with pointer arguments

7. 4 Calling Functions by Reference § § Call by reference with pointer arguments – Pass address of argument using & operator – Allows you to change the actual location in memory – Arrays are not passed with & because the array name is already a pointer. * operator – Used as alias/nickname for variable inside of function void double( int *number ) { *number = 2 * ( *number ); } – *number used as nickname for the variable passed. 2007 Pearson Ed -All rights reserved. Systems Programming: Pointers 10

Using Pointers in Call by Reference 2007 Pearson Ed -All rights reserved. Function prototype

Using Pointers in Call by Reference 2007 Pearson Ed -All rights reserved. Function prototype takes a pointer argument Function cube. By. Reference is passed an address, which can be the value of a pointer variable In this program, *n. Ptr is number, so this statement modifies the value of number itself. Systems Programming: Pointers 11

Swap: A Pointer Example /* A simple memory swap using pointers */ void swap

Swap: A Pointer Example /* A simple memory swap using pointers */ void swap (int *i, int *j) { int temp; } temp = *i; *i = *j; *j = temp; Systems Programming: Pointers 12

Swap: A Pointer Example int main ( ) { int i; int mem 1,

Swap: A Pointer Example int main ( ) { int i; int mem 1, . /swap mem 1: 81 mem 2: 12 ray 1[0] = 0 ray 1[1] = 10 ray 1[2] = 20 ray 1[3] = 30 mem 2, ray 1[4]; mem 1 = 12; mem 2 = 81; swap (&mem 1, &mem 2); /* swap two integers */ printf("mem 1: %4 d mem 2: %4 dn", mem 1, mem 2); for (i = 0; i < 4; i++) { ray 1[i] = 10*i; printf("ray 1[%d] =%4 d ", i, ray 1[i]); } printf("n"); Systems Programming: Pointers 13

Swap: A Pointer Example swap (&mem 1, &ray 1[3]); swap (&mem 2, &ray 1[2]);

Swap: A Pointer Example swap (&mem 1, &ray 1[3]); swap (&mem 2, &ray 1[2]); printf("mem 1: %4 d mem 2: %4 dn", mem 1, mem 2); for (i = 0; i < 4; i++) printf("ray 1[%d] =%4 d ", i, ray 1[i]); } printf("n"); return 0; mem 1: 30 mem 2: 20 ray 1[0] = 0 ray 1[1] = 10 ray 1[2] = 12 ray 1[3] = 81 Systems Programming: Pointers 14

Pointers and Arrays r 1 1 1 0 r[0] r[1] r[2] ptr 0 0

Pointers and Arrays r 1 1 1 0 r[0] r[1] r[2] ptr 0 0 r[5] int main () { int i, r[6] ={1, 1, 1}; int *ptr; ptr = r; *ptr = 83; *(ptr +2) = 33; for (i=0; i < 6; i++) printf (" r[%d] = %dn", i, r[i]); Systems Programming: Pointers 15

Pointers and Arrays r 83 1 33 0 r[0] r[1] r[2] ptr } 0

Pointers and Arrays r 83 1 33 0 r[0] r[1] r[2] ptr } 0 0 r[5] r[4] = *ptr; ptr++; *ptr = 6; *(ptr +2) = 7; for (i=0; i < 6; i++) printf (" r[%d] = %dn", i, r[i]); return 0; Systems Programming: Pointers 16

Pointers and Arrays r 83 6 33 7 r[0] r[1] r[2] ptr } 83

Pointers and Arrays r 83 6 33 7 r[0] r[1] r[2] ptr } 83 0 r[5] r[4] = *ptr; ptr++; *ptr = 6; *(ptr +2) = 7; for (i=0; i < 6; i++) printf (" r[%d] = %dn", i, r[i]); return 0; Systems Programming: Pointers 17

Operator Precedence Example /* An example of operator precedence trouble */ int main ()

Operator Precedence Example /* An example of operator precedence trouble */ int main () { $. /prec float x, y, z; 3220728372 3220728368 3220728364 float *ptr 1, *ptr 2, *ptr 3; x =2. 0; y = 8. 0; z = 4. 0; 2. 000000 8. 000000 4. 000000 3220728372 3220728368 2. 000000 8. 000000 ptr 1 = &x; ptr 2 = &y; ptr 3 = &z; printf (" %u %u %un", ptr 1, ptr 2, ptr 3); *ptr 3++; printf (" %f printf (" %u printf (" %f %f %u %f %fn", %un", %fn", x, y, z); ptr 1, ptr 2, ptr 3); *ptr 1, *ptr 2, *ptr 3); Systems Programming: Pointers 18

Precedence Example (*ptr 1)++; printf (" %f %f %fn", *ptr 1, *ptr 2, *ptr

Precedence Example (*ptr 1)++; printf (" %f %f %fn", *ptr 1, *ptr 2, *ptr 3); } --*ptr 2; printf (" %f %f %fn", *ptr 1, *ptr 2, *ptr 3); return 0; 3. 000000 8. 000000 3. 000000 7. 000000 Systems Programming: Pointers 19

Summary § § This section demonstrated the relationship between pointers and addresses and introduced

Summary § § This section demonstrated the relationship between pointers and addresses and introduced the respective operators & and *. Showed the use of pointers in simple examples. Introduced call by reference with pointers. Detailed the relationship between pointers and arrays. Systems Programming: Pointers 20