记录问题,还没有研究明白
- struct MY_STRUCT{
- int a;
- short b;
- char s[100];
- double d;
- };
- int main(){
- MY_STRUCT s={1,2,"he",999};
- MY_STRUCT* struct_ptr =&s;
- char *char_p =(char *) &s.s;
- int *int_ap = &s.a;
- short * int_bp = &s.b;
- double * int_dp = &s.d;
- qDebug()<<"sizeof(my_struct)" <<sizeof (MY_STRUCT);
- qDebug()<<"char_p = "<
- qDebug()<<"a = "<<*int_ap;
-
- qDebug()<<"int_ap 的地址"<
// +4是b的地址 + 108是d的地址 +8是s的地址 ,内存排序不会安装结构体顺序来 - qDebug()<<"int bp 的地址"<
- qDebug()<<"char的地址"<<&char_p; //和ap相差24个字节,因为内存对其的原因,按照结构体中字节最大的数据的整数倍补齐
- qDebug()<<"int_dp 的地址"<
-
- qDebug()<<"struct的地址"<
// 7993840 - qDebug()<<"struct的地址+1"<
1;// 7993960 相差120个字节,证明按照结构体指针的大小不是4、8,而是实际结构的大小 - qDebug()<<"1 = "<< *((int*)struct_ptr);
- qDebug()<< "2 = "<<*((short*)(struct_ptr+4)) ; //可以取到第二个元素 好像不对? 指针+1是按照指针的大小+1
- qDebug()<< "2= "<<*((short *)(char *)struct_ptr+2);
- // qDebug()<<"3 ="<< ((char *)struct_ptr+1) ; //可以取到第三个元素 104 108 116
- // qDebug()<<"3 ="<< *((double *)(char *)struct_ptr+112) ; //可以取到第三个元素 104 108 116
-
-
- }
最后一部分的偏移还在找规律
-
-
-
- qDebug()<<"struct的地址"<
// 7993840 - qDebug()<<"struct的地址+1"<
1;// 7993960 相差120个字节,证明按照结构体指针的大小不是4、8,而是实际结构的大小 - qDebug()<<"1 = "<< *((int*)struct_ptr);
- // qDebug()<< "2 = "<<*((short*)(struct_ptr+4)) ; //可以取到第二个元素 好像不对? 指针+1是按照指针的大小+1
- // qDebug()<< "2= "<<*((short *)(char *)struct_ptr+2);
- qDebug()<<"2 ="<< *(short *)(((char*)struct_ptr)+4) ;
- qDebug()<<"3 ="<< (char *)(((char *)struct_ptr)+6); //可以先找地址在获取值
- qDebug()<<"4 ="<< *(double *)(((char*)struct_ptr)+16) ;//15偏移24 5偏移16
打印字符数组每个字节的地址
-
- char a[4] = {'i','c','e','\0'};
- for(int i = 0; i < 3; ++i){
- qDebug()<<(void*)a[i] <<" "<<&a[i]<<" " << a[i]; //第一个取到是字符常量的地址
- qDebug()<< (void*)&a[i] ; //获取到指针的地址
- }
-
相关阅读:
计算机网络(自顶向下)—第四章测验题
webpack之路径处理
富格林:应用正规技巧阻挠被骗
策略模式介绍
20221121将行车记录仪记录的MJPEG格式的AVI片段合并的MKV转换为MP4
【Python游戏】用Python实现一个测试你智商的小游戏——24点,过不了三关的都是细狗 | 附带源码
Collection.sort首字母排序
股票买卖接口源码分享
Day41—— 343. 整数拆分 96.不同的二叉搜索树 (动规)
Python文件操作:读取、打开、写入、关闭、按行读取、文件指针(附零基础学习资料)
-
原文地址:https://blog.csdn.net/m0_58233509/article/details/132630928