嵌入式笔试刷题(第28天)
11. 哪个表达式结果为 6?
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int *p = a;
- A.
*p + 6→0 + 6 = 6✅ - B.
*(p + 6)→6✅ - C.
*p += 5→*p = *p + 5 = 5❌ - D.
p + 5→ 地址,不是值 ❌
答案:A、B
12. 设定绝对地址 0x67a9 的变量为 0xaa66
*(int*)0x67a9 = 0xaa66;
注意: 必须在嵌入式或支持裸地址访问的系统上运行(如 STM32),否则会段错误。
13. 宏定义实现
(1)EPRINTF 宏
#define EPRINTF(func, fmt, ...) \
do { \
time_t t = time(NULL); \
struct tm* lt = localtime(&t); \
char buf[32]; \
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", lt); \
fprintf(stderr, "%s [%d @ %s]:%s, " fmt, \
buf, __LINE__, __FILE__, #func, ##__VA_ARGS__); \
} while(0)
调用:
EPRINTF(test(10), "->%s: %d\n", "hello", 2345);
(2)WHEN 宏
#define WHEN(func, act) \
do { \
if ((func) != 0) { \
time_t t = time(NULL); \
struct tm* lt = localtime(&t); \
char buf[32]; \
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", lt); \
fprintf(stderr, "%s [%d @ %s]:%s, %s\n", \
buf, __LINE__, __FILE__, #func, #act); \
act; \
} \
} while(0)
调用:
WHEN(test_failed(), return -1);
14. 带函数指针的结构体示例
#include <stdio.h>
typedef struct {
void (*print)(const char*);
} Printer;
void show(const char* msg) {
printf("Message: %s\n", msg);
}
int main() {
Printer p;
p.print = show;
p.print("Hello struct with function pointer!");
return 0;
}
15. 硬盘对象类与UML类图(文字描述 + C++代码)
对象结构(UML描述):
- Disk has many Partitions
- Partition has many Directories
- Directory has many Files
类代码示例:
class File {
std::string name;
};
class Directory {
std::vector<File> files;
};
class Partition {
std::vector<Directory>
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
嵌入式笔试专栏 文章被收录于专栏
本专栏系统整理了嵌入式方向笔试中常见的知识点和高频考题,涵盖基础理论、常用算法、C语言陷阱、操作系统原理、驱动开发、常见外设通信协议(如 I2C/SPI/UART)、RTOS、Linux 内核、以及实用电路知识等内容。
凡岛公司福利 319人发布