Practice9 - Pointers Pointerisavariablethatcontainsthe...

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

This note was uploaded on 01/10/2010 for the course CS 463 taught by Professor Can'tsay during the Spring '09 term at Haaga - Helia University of Applied Sciences.

Page1 / 84

Practice9 - Pointers Pointerisavariablethatcontainsthe...

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