140 likes | 220 Views
C’ pointers. Basic&Examples. Pointer arithmetic. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i<br>", *(array_ptr++)); printf("second element: %i<br>", *(array_ptr++)); printf(" third element: %i<br>", *array_ptr);
E N D
C’ pointers Basic&Examples
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
Indexing • Q: what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = &array[1]; printf("%i\n", array_ptr[1]); A: 89
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;
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
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
WHAT IS THE ANSWER? #include<stdio.h> intmain(){ inta = 320; char*ptr; ptr =(char*)&a; printf("%d ",*ptr); getch(); return 0; } 64
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
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
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
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
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
WHAT IS THE ANSWER? #include<stdio.h> void main(){ chararr[10]; arr ="world"; printf("%s",arr); getch(); } (A) world (B) Compile error
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