Presentation is loading. Please wait.

Presentation is loading. Please wait.

“Studying C programming excluding pointers is meaningless.” d0m3z

Similar presentations


Presentation on theme: "“Studying C programming excluding pointers is meaningless.” d0m3z"— Presentation transcript:

1 “Studying C programming excluding pointers is meaningless.” d0m3z
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?) // 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;
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; // declaration float **flp; // indirect pointer int a,*x = &a; // x points to a this compact form means int *x; and “ x = &a; ” not “ *x = &a; ”

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

14 Summary: arrays as pointers
char name[4]; scanf(“%s”,name); // pointer when 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 jumps to 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 main int 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?


Download ppt "“Studying C programming excluding pointers is meaningless.” d0m3z"

Similar presentations


Ads by Google