Presentation is loading. Please wait.

Presentation is loading. Please wait.

C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));

Similar presentations


Presentation on theme: "C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));"— Presentation transcript:

1 C’ POINTERS Basic&Examples

2 Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++)); printf("second element: %i\n", *(array_ptr++)); printf(" third element: %i\n", *array_ptr); Ans: first element: 45 second element: 67 third element: 89 P OINTER ARITHMETIC

3 Q: what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = &array[1]; printf("%i\n", array_ptr[1]); A: 89 I NDEXING

4 S TRUCT ’ S PTR struct foo { size_t size; char name[64]; }; struct foo my_foo; my_foo.size = sizeof(struct foo); struct foo *foo_ptr; (*foo_ptr).size = new_size; 或是 foo_ptr->size = new_size; struct foo **foo_ptr_ptr (*foo_ptr_ptr)->size = new_size; 或是 (**foo_ptr_ptr).size = new_size;

5 P OINTERS AND CONST const int *ptr_a; int const *ptr_b; int *const ptr_c; Q: true or false *ptr_a=42; *ptr_b=42; *ptr_c=42; ptr_c++; A: F T F

6 S TRINGS Q:what is the value of len and i? char str[] = "I am the Walrus"; size_t strlen(const char *str) { size_t len = 0; while(*(str++)) ++len; return len; } size_t strlen(const char *str) { size_t i; for(i = 0U; str[i]; ++i); return i; } A: both len and i is 15

7 WHAT IS THE ANSWER? #include int main(){ int a = 320; char *ptr; ptr =( char *)&a; printf("%d ",*ptr); getch(); return 0; } #include int main(){ int a = 320; char *ptr; ptr =( char *)&a; printf("%d ",*ptr); getch(); return 0; } 64

8 WHAT IS THE ANSWER? #include int main(){int i = 3; int *j; int **k; j=&i; k=&j; printf( “ %u %u %d ”,k,*k,**k); return 0; } #include int main(){int i = 3; int *j; int **k; j=&i; k=&j; printf( “ %u %u %d ”,k,*k,**k); return 0; } (A) j Address, i Address,3 (B) j Address, 3, 3 (C) 3, 3, 3 (D) Compilation error (E) None of above (A) j Address, i Address,3 (B) j Address, 3, 3 (C) 3, 3, 3 (D) Compilation error (E) None of above

9 WHAT IS THE ANSWER? #include void main(){ char *ptr1 = NULL; char *ptr2 = 0; strcpy(ptr1," c"); strcpy(ptr2,"questions"); printf("\n%s %s",ptr1,ptr2); getch(); } #include void main(){ char *ptr1 = NULL; char *ptr2 = 0; strcpy(ptr1," c"); strcpy(ptr2,"questions"); printf("\n%s %s",ptr1,ptr2); getch(); } (A) c questions (B) (null) (null) Run error

10 WHAT IS THE ANSWER? #include void main(){ char *p; printf("%d %d",sizeof(p), sizeof(*p) ); getch(); } #include void main(){ char *p; printf("%d %d",sizeof(p), sizeof(*p) ); getch(); } (A) 4 1 (B) Compile error

11 WHAT IS THE ANSWER? #include int main(){ void *k = "\0a\0\0c\0\0\0"; int *p = k; int i; for (i=0; i<2; i++) printf("%d ", *(p+i)); getch(); return 0; } #include int main(){ void *k = "\0a\0\0c\0\0\0"; int *p = k; int i; for (i=0; i<2; i++) printf("%d ", *(p+i)); getch(); return 0; } 24832 99 24832 = 97 X 256

12 WHAT IS THE ANSWER? #include int main(){ void *k = "\0a\0\0c\0\0\0“; k++; printf("%d ", *k); getch(); return 0; } #include int main(){ void *k = "\0a\0\0c\0\0\0“; k++; printf("%d ", *k); getch(); return 0; } k++ and *k : warning printf("%d ", *k) : error

13 WHAT IS THE ANSWER? #include void main(){ char arr[10]; arr = "world"; printf("%s",arr); getch(); } #include void main(){ char arr[10]; arr = "world"; printf("%s",arr); getch(); } (A) world (B) Compile error

14 WHAT IS THE ANSWER? #include void main(){ int a,b,c,d; char *p = ( char *)0; int *q = ( int *)0; float *r = ( float *)0; double *s = 0; a = (int)(p+1); b = (int)(q+1); c = (int)(r+1); d = (int)(s+1); printf("%d %d %d %d",a,b,c,d); } #include void main(){ int a,b,c,d; char *p = ( char *)0; int *q = ( int *)0; float *r = ( float *)0; double *s = 0; a = (int)(p+1); b = (int)(q+1); c = (int)(r+1); d = (int)(s+1); printf("%d %d %d %d",a,b,c,d); } 1 4 4 8


Download ppt "C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));"

Similar presentations


Ads by Google