C语言内存处的强制类型转换

说明:最近学习C语言寄存器操作,读到了代码

#define STOREG(x) (*((uint16_t*)&x)

起初不理解什么意思,现在解释记录一下。

数据类型转换

int a = 1;

int *p = &a;

float *p1 = (float*)p;

指针强制类型转换

int a;

int *p = &a;

char *p1 = (char *)&a;

结构体强制类型转换

struct str1 a;

struct str2 b;

a=*((struct str1*)&b);        

内存强制类型转换

最后,献上我自定义的内存强制类型转换

/*
*@brief     将x强制类型转换成type类型
*@param     x         待转换的变量,是一个结构体
*@param     type      将要转换的类型
*/
#define FORCE_TYPE_CONVERSION(x,type) ( *(type*)&(x) )

/*
*@brief     将x地址强制类型转换成type类型
*@param     x         待转换的变量,是一个地址
*@param     type      将要转换的类型
*/
#define ADDR_STORAGE(x,type) ( *(type*)(x) ) 

 

原文地址:https://www.cnblogs.com/shuoguoleilei/p/14722286.html