大家好,今天给大家介绍C语言指针运算的经典笔试题,文章末尾附有分享大家一个资料包,差不多150多G。里面学习内容、面经、项目都比较新也比较全!可进群免费领取。
以下是一些C语言指针运算的经典笔试题:
-
数组与指针
int array[5] = {1, 2, 3, 4, 5}; int *ptr = array; printf("%d", *(ptr + 2)); // 输出什么?
-
指针与函数
void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int x = 5, y = 10; swap(&x, &y); printf("%d %d\n", x, y); // 输出什么? }
-
指针与结构体
typedef struct { int a; float b; char c; } MyStruct; int main() { MyStruct s = {10, 20.5, 'c'}; MyStruct *p = &s; printf("%p\n", (void*)&s.b); // 输出s.b的地址 printf("%p\n", (void*)(p + 1)); // 输出什么?它与s.b的地址有何关系? }
-
指针运算与内存布局
int main() { int a = 1; double b = 2.0; printf("%p\n", (void*)&a); printf("%p\n", (void*)&b); printf("%ld\n", (long)&b - (long)&a); // 输出什么?为什么? }
-
指针数组与数组指针
int main() { int *arr1[] = { &a, &b, &c }; // 指针数组 int (*arr2)[3] = &array; // 数组指针 int array[3] = {1, 2, 3}; int a = 10, b = 20, c = 30; printf("%d\n", *arr1[1]); // 输出什么? printf("%d\n", (*arr2)[1]); // 输出什么? }
-
野指针与空指针
int *ptr; // ... if (ptr != NULL) { printf("%d\n", *ptr); // 如果ptr没有被初始化,这行代码有什么问题? }
-
动态内存分配
int *ptr = (int*)malloc(10 * sizeof(int)); if (ptr == NULL) { // 错误处理 } // ... free(ptr); // 释放内存后,ptr应该被设置为什么?
-
多级指针
int x = 10; int *p = &x; int **pp = &p; int ***ppp = &pp; printf("%d\n", ***ppp); // 输出什么?
-
指针与字符串
char *str = "Hello, world!"; char str2[] = "Hello, world!"; printf("%p\n", (void*)str); printf("%p\n", (void*)str2); // str 和 str2 有什么不同?它们的地址分别指向哪里?
-
函数指针
int add(int a, int b) { return a + b; } int main() { int (*func_ptr)(int, int) = add; printf("%d\n", func_ptr(5, 3)); // 输出什么? }
这些题目涵盖了指针的基本用法、指针与数组的关系、指针与函数关系、动态内存分配、多级指针以及函数指针等概念。在回答这些问题时,需要深入理解指针在内存中的表示和它们如何影响变量的访问和修改。
C语言要学的东西太多了,千万不要选错学习路线,最后学不精,导致工资要不上去!
我这里整理了一个C语言的学习资料包,里面关于C语言的学习路线、电子书、面试经验、项目都比较新,也比较全!另外还有一套华清小美老师2024年最新录制的C语言课程,源码、课件都是免费开放给大家的!
点击找小助理免费领取资料