代码题 (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;
}
-
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; }
-
int* func() { static int a[5] = {2,4,6,8,10}; return a; } void main() { int *ptr = func(); printf("%d\n", *(ptr + 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;
}
```
-
void modify(int *p) { *p = 20; } void main(){ int a[] = {1,2,3}; modify(a); printf("%d",a[0]);//答案 }
-
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; }
-
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]);//答案 }
-
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; }
-
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)); //答案
}
```
-
#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) ); // 答案:
```
-
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)); //答案: (需要结合数组维度思考)
-
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)); //答案
-
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)); //答案
-
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)); //答案:
```
-
int a[4] = {1, 2, 3, 4}; int *ptr = (int *)(&a + 1); printf("%d", *(ptr-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题)
int *a[10];
a
的类型是什么?int (*a)[10];
a
的类型是什么?int **a;
a
的类型是什么?int a[3][4];
a
的类型是什么?a[0]
的类型是什么?int *p = malloc(10 * sizeof(int));
p
指向的内存区域的大小是多少字节?- 指针的步长由什么决定?
- 数组名在大多数情况下可以被看作是什么?
- 什么是指针数组?
- 什么是数组指针?
- 如何声明一个指向包含5个整数的数组的指针?
- 如何声明一个包含5个指向整数的指针的数组?
int arr[5];
&arr
的类型是什么?int *p;
&p
的类型是什么?void *p;
p
是什么类型的指针?- 可以对
void *
类型的指针进行算术运算吗? NULL
指针的值是什么?- 什么是野指针?
- 什么是悬空指针?
int a[2][3];
表达式a[1]
的含义是什么?int a[5]; int *p = a;
p + 2
和a + 2
的含义相同吗?
答案
代码题答案
- 1
- 2
- 5
- 3
- 4
- 20
- 40
- 40
- 30
- 1 2 3 4 5
- 1 2 3 4 5
- 8
- 5
- 11, 22
- 20
- 2
- 2.2
- 7
- 8 (根据系统,可能是4或8)
- 20
- 5
- 6
- 2
- 6
- 3
- 7
- 4
- 4
- 5
- 4
- 6
- 5
- 3
- 7
- 4 (通常)
- 1
- 8 (通常)
- 8(指针大小,通常), 24(3个int, 3*8=24)
- 6, 8 (指针大小,通常是8)
- 8 (指针大小,通常是8), 24 (3个int的大小,3 * sizeof(int) = 3 * 8 = 24)
- 2
- 2
- 10, 20
- 20, 20
- 3, 3, 2
- 10, 2
- 10
- 2
- 10
- 7, 7
- 7
- 0
- 5
- 5
- 2
- 3
- 3
- 2
- 11
- 20 10
- 3
- 4
- 8
- 4
- 20
- 2 4 6
- 10
- 7
- 10
- 9
- olleh
- 9
- 6
- 11
- 8
- 5
- 10,5
- 3
- 1
- 0xFFFFFFFC, -4 (第一个是地址差的十六进制表示,第二个是十进制, 具体地址值与编译环境相关)
问答题答案
- 指向整数的指针的数组(或:整数指针数组)
- 指向包含10个整数的数组的指针(或:数组指针)
- 指向指针的指针(或:二级指针)
- 包含3个元素的数组,每个元素是包含4个整数的数组; 包含4个整数的数组的类型
- 40 字节 (假设
sizeof(int)
为 4) - 指针指向的数据类型
- 指向数组首元素的指针
- 一个数组,其元素都是指针
- 一个指针,指向一个数组
int (*p)[5];
int *p[5];
- 指向包含5个整数的数组的指针
- 指向指针的指针
- 通用指针/无类型指针
- 不可以,需要先进行类型转换
- 0 (通常)
- 指向未分配内存或已释放内存的指针
- 指向的内存已经被释放,但指针仍然指向该内存区域的指针
a[1]
表示第2个子数组(包含3个int类型元素)的首地址,类型是int [3]
, 也可以看作int*
类型- 相同