几道C语言笔试题及个人理解
2017-08-06 13:21
295 查看
虽然一直想找个Golang相关的工作,也拿了几个offer,但确没有一个心仪的,只能转找C语言了。下面是某新三板上市公司的笔试题,感觉挺有意义的,分享出来。
第0题 本题输出hello,因为GetMemory传入的p是指针。
第1题 本题输出world,因为free只是释放内存,并不等于NULL
第2题 本题无输出,或者输出乱码,因为GetMemory里面的p为局部变量,在GetMemory返回时被释放。
第3题 本题输出hello world;请注意这道题和第2题的区别,第2题的GetMemory中的char p[]是函数的局部变量存储在栈中,随函数返回被释放;本题GetMemory中的“hello world”分配到静态存储区,p指向其,在函数返回时并不会被释放。
具体的请自行搜索引擎char p[] = “hello world”和char *p = “helloworld”的区别。
第0题 本题输出hello,因为GetMemory传入的p是指针。
#include <stdio.h> #include <stdlib.h> #include <string.h> void GetMemory(char **p, int num) { *p = (char *)malloc(num); } void Test(void) { char *str = NULL; GetMemory(&str, 100); strcpy(str, "hello"); printf(str); } void main() { Test(); }
第1题 本题输出world,因为free只是释放内存,并不等于NULL
#include <stdio.h> #include <stdlib.h> #include <string.h> void Test(void) { char *str = (char *)malloc(100); strcpy(str, "hello"); free(str); if (str != NULL) { strcpy(str, "world"); printf(str); } } void main() { Test(); }
第2题 本题无输出,或者输出乱码,因为GetMemory里面的p为局部变量,在GetMemory返回时被释放。
#include <stdio.h> #include <stdlib.h> #include <string.h> char *GetMemory(void) { char p[] = "hello world"; return p; } void Test(void) { char *str = NULL; str = GetMemory(); printf(str); } void main() { Test(); }
第3题 本题输出hello world;请注意这道题和第2题的区别,第2题的GetMemory中的char p[]是函数的局部变量存储在栈中,随函数返回被释放;本题GetMemory中的“hello world”分配到静态存储区,p指向其,在函数返回时并不会被释放。
具体的请自行搜索引擎char p[] = “hello world”和char *p = “helloworld”的区别。
#include <stdio.h> #include <stdlib.h> #include <string.h> char *GetMemory(void) { char *p = "hello world"; return p; } void Test(void) { char *str = NULL; str = GetMemory(); printf(str); } void main() { Test(); }
相关文章推荐
- 个人对c语言的理解
- C语言中的指针-记录个人理解
- 【笔试面试知识点查缺补漏深入理解之C与C++篇】C语言中的引用
- C语言的存储类型的个人理解
- 对c语言中printf函数的个人理解
- 【笔试面试知识点查缺补漏深入理解之C与C++篇】C语言中的malloc和free
- C语言 宏和函数 个人对编译链接的理解
- 【笔试面试知识点查缺补漏深入理解之C与C++篇】C语言组织结构
- c语言 一份驱动开发笔试试题的理解
- 几道经典的嵌入式C语言笔试题
- C语言产生随机数,个人理解
- 几道经典的嵌入式C语言笔试题
- 一道C语言笔试题——理解结构体的内存对齐和位域问题
- 关于c语言二维数组与指针的个人理解及处理办法。
- [C语言] 对于scanf()函数使用%d格式说明符无法读取字符的个人理解
- 关于c语言二维数组与指针的个人理解及处理办法。
- 【笔试面试知识点查缺补漏深入理解之C与C++篇】C语言中的指针数组与数组指针
- 几道经典的嵌入式C语言笔试题
- 【笔试面试知识点查缺补漏深入理解之C与C++篇】C语言中的函数realloc()
- 关于C语言静态链接的个人理解,欢迎指正