代码题 (80题)

以下代码题假设所有代码都包含必要的头文件(如<stdio.h>),并且可以在C语言环境中正确编译和运行。

Level 1: 基础 (40题)


int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
printf("%d\n", *p); // 答案:

int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
printf("%d\n", *(p + 1)); // 答案:

int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
printf("%d\n", *(p + 4)); // 答案:

int arr[5] = {1, 2, 3, 4, 5};
int *p = &arr[2];
printf("%d\n", *p); // 答案:

int arr[5] = {1, 2, 3, 4, 5};
int *p = arr + 3;
printf("%d\n", *p); // 答案:

int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;
p++;
printf("%d\n", *p); // 答案:

int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;
p = p + 3;
printf("%d\n", *p); // 答案:

int arr[5] = {10, 20, 30, 40, 50};
int *p = &arr[4];
p--;
printf("%d\n", *p); // 答案:

int arr[5] = {10, 20, 30, 40, 50};
int *p = &arr[4];
p = p - 2;
printf("%d\n", *p); // 答案:

int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
for (int i = 0; i < 5; i++) {
    printf("%d ", *(p + i));
} // 答案:

int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
for (int i = 0; i < 5; i++) {
    printf("%d ", *p++);
} // 答案:

  int arr[] = {2,4,6,8,10};
  int *ptr = arr;
  printf("%d\n", *(ptr + 3)); // 答案:

int numbers[] = {1, 3, 5, 7, 9};
int *ptr = numbers;
ptr += 2;
printf("%d\n", *ptr); //答案:

int values[] = {11, 22, 33, 44, 55};
int *ptr = values;
printf("%d\n", *(ptr++)); //答案:
printf("%d\n", *ptr); //答案:

int data[] = {5, 10, 15, 20, 25};
int *ptr = data + 4;
printf("%d\n", *(--ptr)); // 答案:

 int arr[] = {1,2,3,4,5};
 int *p = &arr[3];
 printf("%d\n", p[-2]);//答案

 float arr[] = {1.1, 2.2, 3.3, 4.4};
 float *p = arr + 2;
 printf("%.1f\n", *(p-1)); //答案

int arr[] = {1,3,5,7,9};
int *p = &arr[2];
printf("%d\n",p[1]);//答案

  int arr[] = {10,20,30,40,50};
  int *ptr = arr;
  printf("%ld\n", sizeof(ptr)); //答案:

  int arr[] = {10,20,30,40,50};
  printf("%ld\n", sizeof(arr)); //答案:

int arr[][3] = {{1, 2, 3}, {4, 5, 6}};
printf("%d\n", *(*(arr + 1) + 1)); // 答案:

int arr[][3] = {{1, 2, 3}, {4, 5, 6}};
int (*p)[3] = arr;
printf("%d\n", *(*(p + 1) + 2)); // 答案:

 int arr[2][2] = {{1,2},{3,4}};
 printf("%d", *(*arr + 1));//答案

 int arr[3][2] = {{1,2},{3,4},{5,6}};
 int (*p)[2] = arr;
 printf("%d", *(*(p+2)+1)); //答案

  int arr[2][3] = {{1,2,3},{4,5,6}};
  int (*ptr)[3] = arr;
  printf("%d\n", *(*(ptr)+2)); //答案:

int arr[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
printf("%d\n", *(*(*(arr + 1) + 1) + 0)); // 答案:

  int arr[2][2][2] = {{{1,2},{3,4}},{{5,6},{7,8}}};
  int *p = &arr[0][0][0];
  printf("%d\n", *(p+3)); //答案:

  int a[3][2] = {{1,2},{3,4},{5,6}};
  int *p = a[1];
  printf("%d\n", p[1]); //答案:

int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *p = &arr[0][0];
printf("%d\n", *(p + 4)); //答案:

int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *p = arr[0];
printf("%d\n", *(p + 3)); //答案:

 int arr[2][3] = {1,2,3,4,5,6};
 int *p = *arr;
 printf("%d\n", *(p+5)); //答案

int matrix[3][2] = {{1, 2}, {3, 4}, {5, 6}};
int *ptr = &matrix[1][0];
printf("%d\n", *(ptr + 2)); //答案:

int a[2][2]={{1,2},{3,4}};
int (*p)[2] = a;
p++;
printf("%d",**p);//答案

    int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
    int (*p)[3] = a;
    p+=2;
    printf("%d",**p); //答案

int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
printf("%ld\n", p);
printf("%ld\n", p + 1); // 观察输出,计算步长
// 步长:
char arr[5] = {'a', 'b', 'c', 'd', 'e'};
char *p = arr;
printf("%ld\n", p);
printf("%ld\n", p + 1); // 观察输出,计算步长
// 步长:
double arr[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
double *p = arr;
printf("%ld\n", p);
printf("%ld\n", p + 1); // 观察输出,计算步长
// 步长:
  int a[2][3]={{1,2,3},{4,5,6}};
  int (*p)[3] = a;
  printf("%ld\n",sizeof(p));//答案:
  printf("%ld\n",sizeof(*p));//答案

     char s[] = "hello";
     char *p = s;
     printf("%ld %ld\n", sizeof(s), sizeof(p)); // 答案:
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*p)[3] = arr;
printf("%ld\n", sizeof(p));
printf("%ld\n", sizeof(*p)); // 答案:

Level 2: 进阶 (30题)

int a = 1, b = 2, c = 3;
int *arr[3] = {&a, &b, &c};
printf("%d\n", *arr[1]); // 答案:
int a = 1, b = 2, c = 3;
int *arr[] = {&a, &b, &c};
int **p = arr;
printf("%d\n", *(*(p + 1))); // 答案:

int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;
printf("%d\n", *ptr++);  // 答案:
printf("%d\n", *ptr);    // 答案:

int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;
printf("%d\n", *++ptr); // 答案:
printf("%d\n", *ptr);   // 答案:
```c

    int nums[] = {1, 2, 3, 4, 5};
    int *p = nums + 3;
    printf("%d\n", *(--p)); // 答案
    printf("%d\n",*(p--));//答案
    printf("%d\n", *p); //答案
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
*p++ = 10;
printf("%d, %d\n", arr[0], *p); // 答案:
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
*(p + 2) = 10;
printf("%d\n", arr[2]); // 答案:
int arr[] = {1,2,3,4,5};
int *p = arr;
(*p)++;
printf("%d", *p); //答案
 int arr[] = {1,2,3,4,5};
 int *p = &arr[2];
 *p = 10;
 printf("%d",arr[2]);//答案

int arr[] = {5, 10, 15, 20};
int *ptr = arr;
*ptr += 2; 
printf("%d\n", *ptr); // 答案:
printf("%d\n", arr[0]); //答案

```c

int arr[2][3]={{1,2,3},{4,5,6}};
int *p = &arr[0][0];
*(p + 2) = 7;
 printf("%d", arr[0][2]); //答案
    int arr[3][2] = {{1,2},{3,4},{5,6}};
    int *p = arr[1];
    p[0] = 0;
    printf("%d\n", arr[1][0]);//答案
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *ptr = &matrix[0][0];
*(ptr + 4) = 10;
printf("%d\n", matrix[1][1]); //答案:

    int arr[2][2] = {{10, 20}, {30, 40}};
      int *p = *arr;
      p[3] = 5;
      printf("%d\n", arr[1][1]);//答案:
int arr[5] = {1, 2, 3, 4, 5};
int *p = &arr[1];
int *q = &arr[3];
printf("%ld\n", q - p); // 答案:
char str[] = "Hello";
char *p = str;
char *q = str + 3;
printf("%ld\n", q - p); // 答案:
int arr[] = {1,2,3,4,5,6};
int *p = arr + 4;
int *q = arr + 1;
printf("%ld\n",p-q); //答案
      double arr[] = {1.1, 2.2, 3.3, 4.4};
      double *p1 = &arr[1];
      double *p2 = &arr[3];
      printf("%ld\n",p2 - p1); //答案:
void increment(int *p) {
    (*p)++;
}
int main() {
    int a = 10;
    increment(&a);
    printf("%d\n", a); // 答案:
    return 0;
}
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
int main() {
    int x = 10, y = 20;
    swap(&x, &y);
    printf("%d %d\n", x, y); // 答案:
    return 0;
}

    int* getArray() {
        static int arr[5] = {1, 2, 3, 4, 5};
        return arr;
    }
    int main() {
        int *p = getArray();
        printf("%d\n", *(p + 2)); // 答案:
        return 0;
    }
  1. int (*getArr())[3] {
        static int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
        return arr;
    }
    int main(){
      int (*p)[3] = getArr();
      printf("%d",*(*(p+1)+0)); //答案
      return 0;
    }
    
  2. int* func() {
      static int a[5] = {2,4,6,8,10};
      return a;
    }
    void main() {
        int *ptr = func();
        printf("%d\n", *(ptr + 3)); // 答案:
    }
    
  3. int (*foo())[2] {
        static int bar[3][2] = {{1, 2}, {3, 4}, {5, 6}};
        return bar + 1;
    }
    int main() {
        int (*ptr)[2] = foo();
        printf("%d\n", *(*(ptr) + 1)); // 答案:
        return 0;
    

}

```
  1. void modify(int *p) {
      *p = 20;
    }
    void main(){
      int a[] = {1,2,3};
      modify(a);
      printf("%d",a[0]);//答案
    }
    
  2. void modifyArray(int *arr, int size) {
        for (int i = 0; i < size; i++) {
            arr[i] *= 2;
        }
    }
    int main() {
        int arr[3] = {1, 2, 3};
        modifyArray(arr, 3);
        printf("%d %d %d\n", arr[0], arr[1], arr[2]); // 答案:
        return 0;
    }
    
  3. void doubleValues(int arr[][3], int rows) {
      for(int i = 0; i < rows; i++){
        for(int j = 0; j<3;j++){
          arr[i][j] *= 2;
        }
      }
    }
    
    void main(){
        int a[2][3] = {{1,2,3},{4,5,6}};
        doubleValues(a,2);
        printf("%d", a[1][1]);//答案
    }
    
  4. void update(int arr[][2], int rows) {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < 2; j++) {
                arr[i][j] += 5;
            }
        }
    }
    int main() {
        int matrix[2][2] = {{1, 2}, {3, 4}};
        update(matrix, 2);
        printf("%d\n", matrix[0][1]); //答案:
        return 0;
    }
    
  5. int sumArray(int *arr, int size) {
        int sum = 0;
        for (int i = 0; i < size; i++) {
            sum += *(arr + i);
        }
        return sum;
    }
    int main() {
        int arr[4] = {1, 2, 3, 4};
        printf("%d\n", sumArray(arr, 4)); // 答案:
        return 0;
    }
    
int findMax(int *arr, int size) {
  int max = *arr;
  for(int i = 1; i < size; i++){
    if(*(arr + i) > max){
      max = *(arr + i);
    }
  }
  return max;
}

void main(){
  int a[] = {3,1,4,1,5,9};
  printf("%d\n", findMax(a,6)); //答案
}
```
  1. #include <string.h>
    void reverseString(char *str) {
        int len = strlen(str);
        char *start = str;
        char *end = str + len - 1;
        while (start < end) {
            char temp = *start;
            *start = *end;
            *end = temp;
            start++;
            end--;
        }
    }
    int main() {
        char str[] = "hello";
        reverseString(str);
        printf("%s\n", str); // 答案:
        return 0;
    }
    

Level 3: 难题 (10题)

int arr[3][2][2] = { {{1,2},{3,4}}, {{5,6},{7,8}}, {{9,10},{11,12}} };
int (*p)[2][2] = arr;
printf("%d\n", *(*(*(p+1)+1)+0) ); // 答案:
```
  1. int arr[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
    int *p = &arr[0][0][0];
    printf("%d\n", *(p + 2 * 2 * 1 + 1)); //答案: (需要结合数组维度思考)
    
  2. int a[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
    int *p = a[0];
    printf("%d\n", *(p + 3*4 -2)); //答案
    
  3. int arr[3][2][2] = {1,2,3,4,5,6,7,8,9,10,11,12};
    int *p = *(*arr);
    printf("%d\n", *(p+7)); //答案
    
  4. int a[5] = {1, 2, 3, 4, 5};
    int *p = (int*)(&a + 1);
    printf("%d\n", *(p - 1)); // 答案:
    
int aa[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int *ptr1 = (int *)(&aa + 1);
int *ptr2 = (int *)(*(aa + 1));
printf( "%d,%d", *(ptr1 - 1), *(ptr2 - 1));  //答案:
```
  1. int a[4] = {1, 2, 3, 4};
    int *ptr = (int *)(&a + 1);
    printf("%d", *(ptr-2)); //答案
    
  2. int a[3][2] = { (0, 1), (2, 3), (4, 5) }; // 注意逗号表达式
    int *p;
    p = a[0];
    printf("%d\n", p[0]); //答案:
    
    C
    
    int a[5][5];
    int (*p)[4];
    p = (int (*)[4])a; // 强制类型转换
    printf("%p, %d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]); //答案:
    

问答题 (20题)

  1. int *a[10]; a 的类型是什么?
  2. int (*a)[10]; a 的类型是什么?
  3. int **a; a 的类型是什么?
  4. int a[3][4]; a 的类型是什么? a[0]的类型是什么?
  5. int *p = malloc(10 * sizeof(int)); p 指向的内存区域的大小是多少字节?
  6. 指针的步长由什么决定?
  7. 数组名在大多数情况下可以被看作是什么?
  8. 什么是指针数组?
  9. 什么是数组指针?
  10. 如何声明一个指向包含5个整数的数组的指针?
  11. 如何声明一个包含5个指向整数的指针的数组?
  12. int arr[5]; &arr 的类型是什么?
  13. int *p; &p 的类型是什么?
  14. void *p; p是什么类型的指针?
  15. 可以对void *类型的指针进行算术运算吗?
  16. NULL 指针的值是什么?
  17. 什么是野指针?
  18. 什么是悬空指针?
  19. int a[2][3]; 表达式a[1]的含义是什么?
  20. int a[5]; int *p = a; p + 2a + 2 的含义相同吗?

答案

代码题答案

  1. 1
  2. 2
  3. 5
  4. 3
  5. 4
  6. 20
  7. 40
  8. 40
  9. 30
  10. 1 2 3 4 5
  11. 1 2 3 4 5
  12. 8
  13. 5
  14. 11, 22
  15. 20
  16. 2
  17. 2.2
  18. 7
  19. 8 (根据系统,可能是4或8)
  20. 20
  21. 5
  22. 6
  23. 2
  24. 6
  25. 3
  26. 7
  27. 4
  28. 4
  29. 5
  30. 4
  31. 6
  32. 5
  33. 3
  34. 7
  35. 4 (通常)
  36. 1
  37. 8 (通常)
  38. 8(指针大小,通常), 24(3个int, 3*8=24)
  39. 6, 8 (指针大小,通常是8)
  40. 8 (指针大小,通常是8), 24 (3个int的大小,3 * sizeof(int) = 3 * 8 = 24)
  41. 2
  42. 2
  43. 10, 20
  44. 20, 20
  45. 3, 3, 2
  46. 10, 2
  47. 10
  48. 2
  49. 10
  50. 7, 7
  51. 7
  52. 0
  53. 5
  54. 5
  55. 2
  56. 3
  57. 3
  58. 2
  59. 11
  60. 20 10
  61. 3
  62. 4
  63. 8
  64. 4
  65. 20
  66. 2 4 6
  67. 10
  68. 7
  69. 10
  70. 9
  71. olleh
  72. 9
  73. 6
  74. 11
  75. 8
  76. 5
  77. 10,5
  78. 3
  79. 1
  80. 0xFFFFFFFC, -4 (第一个是地址差的十六进制表示,第二个是十进制, 具体地址值与编译环境相关)

问答题答案

  1. 指向整数的指针的数组(或:整数指针数组)
  2. 指向包含10个整数的数组的指针(或:数组指针)
  3. 指向指针的指针(或:二级指针)
  4. 包含3个元素的数组,每个元素是包含4个整数的数组; 包含4个整数的数组的类型
  5. 40 字节 (假设 sizeof(int) 为 4)
  6. 指针指向的数据类型
  7. 指向数组首元素的指针
  8. 一个数组,其元素都是指针
  9. 一个指针,指向一个数组
  10. int (*p)[5];
  11. int *p[5];
  12. 指向包含5个整数的数组的指针
  13. 指向指针的指针
  14. 通用指针/无类型指针
  15. 不可以,需要先进行类型转换
  16. 0 (通常)
  17. 指向未分配内存或已释放内存的指针
  18. 指向的内存已经被释放,但指针仍然指向该内存区域的指针
  19. a[1] 表示第2个子数组(包含3个int类型元素)的首地址,类型是 int [3] , 也可以看作 int* 类型
  20. 相同