嵌入式笔试刷题(第43天)
第1题:输出结果分析
main() { char str[] = "world"; cout << sizeof(str) << ":"; char *p = str; cout << sizeof(p) << ":"; char i = 10; cout << sizeof(i) << ":"; void *pp = malloc(10); cout << sizeof(pp) << endl; }
答案:6:8:1:8
解析:
str[]
是数组,长度为 6("world\0"),所以sizeof(str)
是 6;p
是指针,64 位系统下为 8 字节;i
是char
类型,占 1 字节;pp
是void*
,也是指针,占 8 字节。
第2题:类的静态成员变量行为
#include<iostream> class test{ static int count; public: test() { count++; } ~test() { count--; } static int getCount() { return count; } }; int test::count=0; int main() { test *p = new test; test *q = new test; delete p; std::cout << "count=" << test::getCount() << std::endl; return 0; }
答案:count=1
解析:构造了两个对象 p
和 q
,调用两次构造函数,count = 2
;delete p;
后析构函数调用一次,count = 1
。
第3题:字符串反转函数
void reverseString(char* str) { int n = strlen(str); for(int i=0; i<n/2; i++) { char t = str[i]; int k = n - 1 - i; str[i] = str[k]; str[k] = t; } }
答案:k = n - 1 - i
解析:实现反转需要将对称位置的字符交换。str[0]
与 str[n-1]
,str[1]
与 str[n-2]
...
第4题:输出结果分析
#include <stdio.h> main() { int a[] = {1,2,3,4,5,6}; int *p = a + 1; int s = ++(*p); s = s + (*++p); printf("%d", s); }
答案:7
解析:
p = a + 1
,指向值为 2;++(*p)
将 2 改为 3,s = 3;++p
指向 3,*p
是 3,s = 3 + 3 = 6;- 实际打印为 6,但根据源码中是否初始值为 (语法
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
嵌入式笔试专栏 文章被收录于专栏
本专栏系统整理了嵌入式方向笔试中常见的知识点和高频考题,涵盖基础理论、常用算法、C语言陷阱、操作系统原理、驱动开发、常见外设通信协议(如 I2C/SPI/UART)、RTOS、Linux 内核、以及实用电路知识等内容。