270 likes | 512 Views
C Pointers. “Studying C programming excluding pointers is meaningless.” d0m3z. Ex1: referencing. void main() { int a; // allocation a = 6; // initialization printf(" %d <br>", a); printf(" %p ", &a); // referencing }. Ex2 : dereferencing. void main() {
E N D
C Pointers “Studying C programming excluding pointers is meaningless.” d0m3z
Ex1: referencing void main() { int a; // allocation a = 6; // initialization printf(" %d \n", a); printf(" %p ", &a); // referencing }
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 }
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); }
Ex4: void main() { int a = 5, b = 2, *x; x = &a; (*x)++; printf(" %d \n", a); x = &b; printf(" %d \n", (*x)--); }
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 }
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)); }
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++); }
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]); }
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); }
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] ); }
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; ”
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);
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])
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++; ”
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);
Summary: pointers with functions • void incre(int *a) { (*a)++;}// in the mainint num = 6; incre(&num);“&num” does not change, but “num” may change.
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?