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

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

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

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

View Full Document
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.
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

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

View Full Document
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
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

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

View Full Document
Example                   pointers.c
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

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

View Full Document
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
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

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

View Full Document
pointers.c – step by step
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
Ask a homework question - tutors are online