Practice9 - Pointers addressofavariable C 7 172 173 174 3...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Pointers Pointer is a variable that contains the  address of a variable Here P is said to  point  to the variable C C 7 3 4 173 172 174 175 176 177 178 179 180 181 174 3 4 P 833 832 834 835 836 837 838 839 840 841
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Referencing The unary operator & gives the  address of a variable The statement                              P=&C     assigns the address of C to the variable P, and now P points to C To print a pointer, use %p format.
Background image of page 2
Referencing int C; int *P; /* Declare P as a pointer to int */ C = 7; P = &C; C 7 3 4 173 172 174 175 176 177 178 179 180 181 174 3 4 P 833 832 834 835 836 837 838 839 840 841
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Dereferencing The unary operator * is the  dereferencing operator Applied on pointers Access the object the pointer points to The statement                     *P=5;      Puts in C (the variable pointed by P) the value 5
Background image of page 4
Dereferencing printf(“%d”, *P); /* Prints out ‘7’ */ *P = 177; printf(“%d”, C); /* Prints out ‘177’ */ P = 177; /* This is unadvisable! */ C 7 3 4 173 172 174 178 179 180 181 174 3 4 833 832 834 835 836 837 838 839 840 841 177 177
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Example                   pointers.c
Background image of page 6
pointers.c – step by step int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x;    /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip;    /* y is now 1 */ printf("y is now %d\n",y); *ip = 0;    /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2];  /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1;  /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); x y z ip 1 2 364 5 6 7 Z[0] Z[1] Z[2] 120 248 364 368 372 564 772
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
pointers.c – step by step int  x=1, y=2, z[10]={5,6,7}; int  *ip;  /* ip is a pointer to int */ ip = &x;   /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip;    /* y is now 1 */ printf("y is now %d\n",y); *ip = 0;    /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2];  /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1;  /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); x y z ip 1 2 364 5 6 7 Z[0] Z[1] Z[2] 120 248 364 368 372 120 564 772
Background image of page 8
pointers.c – step by step int  x=1, y=2, z[10]={5,6,7}; int  *ip;  /* ip is a pointer to int */ ip = &x;    /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip;    /* y is now 1 */ printf("y is now %d\n",y); *ip = 0;    /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2];  /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1;  /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); x y z ip 1 2 364 5 6 7 Z[0] Z[1] Z[2] 120 248 364 368 372 120 564 772
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
pointers.c – step by step
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 84

Practice9 - Pointers addressofavariable C 7 172 173 174 3...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online