1 / 14

C’ pointers

C’ pointers. Basic&amp;Examples. Pointer arithmetic. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(&quot; first element: %i<br>&quot;, *(array_ptr++)); printf(&quot;second element: %i<br>&quot;, *(array_ptr++)); printf(&quot; third element: %i<br>&quot;, *array_ptr);

lizina
Download Presentation

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 Basic&Examples

  2. Pointer arithmetic 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

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

  4. Struct’sptr • 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. Pointers 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 • F • T • F

  6. Strings • 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<stdio.h> intmain(){ inta = 320; char*ptr; ptr =(char*)&a; printf("%d ",*ptr); getch(); return 0; } 64

  8. WHAT IS THE ANSWER? #include<stdio.h> intmain(){inti = 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<stdio.h> #include<string.h> voidmain(){ 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<stdio.h> #include<string.h> voidmain(){ char*p; printf("%d %d",sizeof(p), sizeof(*p) ); getch(); } (A) 4 1 (B) Compile error

  11. WHAT IS THE ANSWER? #include<stdio.h> #include<string.h> intmain(){ void *k = "\0a\0\0c\0\0\0"; int *p = k; inti; 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<stdio.h> #include<string.h> intmain(){ 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<stdio.h> void main(){ chararr[10]; arr ="world"; printf("%s",arr); getch(); } (A) world (B) Compile error

  14. WHAT IS THE ANSWER? #include<stdio.h> #include<string.h> voidmain(){ inta,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

More Related