宏定义
取出指定内存地址上单/双字节数据
实验环境
- 编辑器-->vscode
- 编译器-->gcc
测试代码
#include <stdio.h>
#define MEM_BYTE(x) (*((unsigned char *)(x)))
#define MEM_WORD(x) (*((unsigned short *)(x)))
int main(void)
{
unsigned char *p = NULL;
unsigned short *p1 = NULL;
unsigned char test_var = 0;
unsigned char A = 100;
unsigned short B = 200;
/* 获取变量A的内存地址 */
p = &A;
printf("The address of A is %p\n", p);
/*获取变量B的内存地址*/
p1 = &B;
printf("The address of B is %p\n", p1);
/* 获取address上的一个字节数据 */
test_var = MEM_BYTE(p);
printf("The value of test_var is %#x\n", test_var);
/* 获取address上的两个字节数据 */
test_var = MEM_WORD(p1);
printf("The value of test_var is %#x\n", test_var);
}