1 / 18

C Pointers

C Pointers. “Studying C programming excluding pointers is meaningless.” d0m3z. Ex1: referencing. void main() { int a; // allocation a = 6; // initialization printf(&quot; %d <br>&quot;, a); printf(&quot; %p &quot;, &amp;a); // referencing }. Ex2 : dereferencing. void main() {

magar
Télécharger la présentation

C Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Pointers “Studying C programming excluding pointers is meaningless.” d0m3z

  2. Ex1: referencing void main() { int a; // allocation a = 6; // initialization printf(" %d \n", a); printf(" %p ", &a); // referencing }

  3. Ex2: dereferencing void main() { char n[8] = "its050"; printf(" %c %c \n", n[0], n[1]); printf(" %p %p \n", &n[0], &n[1]); printf(" %c \n", *(&n[0]) ); // dereferencing operator }

  4. Ex3: pointer declaration void main() { int a = 6; int *x; // not dereferencing // must be of type what it points to x = &a; printf(" %p %p \n", x, &a); printf(" %d %d \n", *x, a); }

  5. Ex4: void main() { int a = 5, b = 2, *x; x = &a; (*x)++; printf(" %d \n", a); x = &b; printf(" %d \n", (*x)--); }

  6. Ex5: null pointer, compact form; void main() { char a = '6', *x = &a; // compact // char *x; and x = &a; printf(" %c ",*x); x = NULL; // x = 0; is also allowed }

  7. Ex6: arrays/strings as pointers void main() { char n[8] = "its050", *x = &n[0]; // “ n ” is defined as “ &n[0] ” printf(" %p %p \n", n, x); printf(" %c %c \n", *n, *x); printf(" %c %c \n", *(n+1), *(x+1)); }

  8. Ex7: de/increment void main() { int num[] = {1,1,2,3}, *x = num; printf(" %d %d", *x, *(x+2) ); x++; //jump 4 bytes (how?) printf(" %d %d", *x, *(x+2) ); // what if printf(“ %p %p ”, num+1, num++); }

  9. Ex8: void main() { int a[4] = {1,1,2,3}; int *p = a, *q = p+2; *p = *(a+3) + *q; q++; *q = *(p+1); printf("%d %d %d %d",*a,a[1],*(a+2),a[3]); }

  10. Ex9: parameters of a function void foo(int a,int *b) { a++; *b *= a; } void main() { int a = 5, b = 6, *x = &a; foo(a, &b); printf("%d %d\n",a ,b); foo(b,x); printf("%d %d\n",a ,b); }

  11. Ex10: int foo(int a,int *b) { a = (*b +1); *(b+1) = a; return a; } void main() { int n[] = {2,4,5,8}, a = 0; a = foo(a,n); printf("%d %d %d",*(&a),*b,b[1] ); }

  12. Summary: declaration • char *ptr; // declarationfloat **flp; // indirect pointer • int a,*x = &a; // x points to athis compact form meansint *x; and“ x = &a; ”not “ *x = &a; ”

  13. Summary: de/referencing • &x; // the address of the variable xint *x,a = 5;x = &a; • *x; // the value at the location xchar *x,b[2] = {‘e’,’x’};x = b+1;printf(“ %c ”, *x);

  14. Summary: arrays as pointers • char name[4];scanf(“%s”,name); // pointerwhen an array is called without index, it is a pointer.“name” actually means &(name[0])

  15. Summary: de/increment • int a[4] = {1,1,2,3};int x = a+2;printf(“ %d ”,*x++);the pointer intuitively jumpsto the next location. Why? • What happens if “ a++; ”

  16. Summary: pointers with functions • void incre(int a) { a++;}whatever is passed as a parameter,its value will never change.int num = 6; incre(num);

  17. Summary: pointers with functions • void incre(int *a) { (*a)++;}// in the mainint num = 6; incre(&num);“&num” does not change, but “num” may change.

  18. Quick Checks • You should be able to answer these questions now! (not in exam)Simply storing addresses, Why are pointers categorized? int*, char*Why do we need an & in scanf?

More Related