定义的结构体
//结构体
typedef struct ___STRUCT
{
int i_0;
int i_1;
char c_0[20];
char c_1[20];
int i_2;
int i_3;
}STRUCT;
操作
int main(int argc,const char** argv)
{
STRUCT _struct;
int* pointer_i = (int*)&_struct;//定义一个int型指针执行_struct的地址
*pointer_i = 99;//结构体地址也是里面第一个变量的地址
*(pointer_i + 1) = 99;//利用地址偏移获取第二个变量的地址,解引用后可以进行赋值
char* pointer_c = (char*)(pointer_i + 2);//第三个变量是一个char型数组,因为指针是指向第二个变量,需要利用偏移过到第三个变量的位置
strcpy(pointer_c,"hello world");//直接进行赋值
strcpy(pointer_c + 20,"hi world");//因为数组长度为20,因此偏移时需要偏移20
pointer_i = (int*)(pointer_c + 20 + 20);//过掉两个数组就到最后一个变量
*pointer_i = 99;//直接解引用就可以赋值了
*((int*)((char*)((int*)&_struct + 2) + 40) + 1) = 99;
printf("%d,%d,%s,%s,%d,%d\n", _struct.i_0,
_struct.i_1,
_struct.c_0,
_struct.c_1,
_struct.i_2,
_struct.i_3);
return 0;
}
通过指针赋值结构体的操作可以很好的理解结构体的内存结构