当前位置:网站首页>C语言数组与结构体指针
C语言数组与结构体指针
2022-06-25 00:32:00 【呼拉拉啦啦啦啦】
C语言数组与结构体指针
在使用修改芯片官方源码的时候,看到官方采用了把结构体指针赋值到数组上的做法,自己做了一番修改,却总是有些问题。示例如下:
#include <stdio.h>
#include <ctype.h>
struct sss1{
int s1;
int s2;
int s3;
};
struct sss2 {
int s4;
int s5;
int s6;
};
int main(int argc, char *argv[])
{
int sss[100];
struct sss1 *sss_1;
struct sss2 *sss_2;
sss_1=(struct sss1 *)sss;
sss_1->s1=1;
sss_1->s2=2;
sss_1->s3=3;
sss_2=(struct sss2 *)(sss+sizeof(struct sss1));
sss_2->s4=4;
sss_2->s5=5;
sss_2->s6=6;
printf("sss ADD 0X%08X\n",sss);
printf("sss_1 ADD 0X%08X\n",sss_1);
printf("sizeof(struct sss1) 0X%08X\n",sizeof(struct sss1));
printf("sss_2 ADD 0X%08X\n",sss_2);
for(int i=0;i<20;i++)
{
printf("sss DATA[%d] 0x%hx\r\n",i,*(sss+i));
}
return 0;
}
输出结果如下:
sss ADD 0X06CD9100
sss_1 ADD 0X06CD9100
sizeof(struct sss1) 0X0000000C
sss_2 ADD 0X06CD9130
sss DATA[0] 0x1
sss DATA[1] 0x2
sss DATA[2] 0x3
sss DATA[3] 0x0
sss DATA[4] 0x0
sss DATA[5] 0x0
sss DATA[6] 0x0
sss DATA[7] 0x0
sss DATA[8] 0x0
sss DATA[9] 0x0
sss DATA[10] 0x0
sss DATA[11] 0x0
sss DATA[12] 0x4
sss DATA[13] 0x5
sss DATA[14] 0x6
sss DATA[15] 0x0
sss DATA[16] 0x0
sss DATA[17] 0x0
sss DATA[18] 0x0
sss DATA[19] 0x0
这种操作方式还是很方便的,对于不同数据的叠加
这里有两个问题
1:sss_2的地址为什么不是 0X06CD9100 + 0X0000000C =0X06CD910C
2: sss数组中间3-11是怎么空出来的?
我查了下,有两个知识点
1,sizeof()返回的是该类型的字节数即nchar,结构体sss1包含3个int,64位系统(4char(16 bit))即34个char,所以大小是C=12.
2,指针的加减操作不同于数字的简单加减。指针±数字=指针值+指针类型大小数字。
这也解释了,为什么sss2的地址偏移了12个int。
修改的方法我这边是在赋值sss_2的时候把sss指针定义成char类型的指针,如下:
#include <stdio.h>
#include <ctype.h>
struct sss1{
int s1;
int s2;
int s3;
};
struct sss2 {
int s4;
int s5;
int s6;
};
int main(int argc, char *argv[])
{
int sss[100];
struct sss1 *sss_1;
struct sss2 *sss_2;
sss_1=(struct sss1 *)sss;
sss_1->s1=1;
sss_1->s2=2;
sss_1->s3=3;
sss_2=(struct sss2 *)((char *)sss+sizeof(struct sss1));
sss_2->s4=4;
sss_2->s5=5;
sss_2->s6=6;
printf("sss ADD 0X%08X\n",sss);
printf("sss_1 ADD 0X%08X\n",sss_1);
printf("sizeof(struct sss1) 0X%08X\n",sizeof(struct sss1));
printf("sizeof(char) 0X%08X\n",sizeof(char));
printf("sizeof(int) 0X%08X\n",sizeof(int));
printf("sss_2 ADD 0X%08X\n",sss_2);
for(int i=0;i<20;i++)
{
printf("sss DATA[%d] 0x%hx\r\n",i,*(sss+i));
}
return 0;
}
输出结果如下:
sss ADD 0X0AE6FFA0
sss_1 ADD 0X0AE6FFA0
sizeof(struct sss1) 0X0000000C
sizeof(char) 0X00000001
sizeof(int) 0X00000004
sss_2 ADD 0X0AE6FFAC
sss DATA[0] 0x1
sss DATA[1] 0x2
sss DATA[2] 0x3
sss DATA[3] 0x4
sss DATA[4] 0x5
sss DATA[5] 0x6
sss DATA[6] 0x0
sss DATA[7] 0x0
sss DATA[8] 0x0
sss DATA[9] 0x0
sss DATA[10] 0x0
sss DATA[11] 0x0
sss DATA[12] 0x0
sss DATA[13] 0x0
sss DATA[14] 0x0
sss DATA[15] 0x0
sss DATA[16] 0x0
sss DATA[17] 0x0
sss DATA[18] 0x0
sss DATA[19] 0x0
基础不牢,想不明白了好久,记录下吧。
边栏推荐
- AI clothing generation helps you complete the last step of clothing design
- 微信小程序获取扫描二维码后携带的参数
- Introduction to CUDA Programming minimalist tutorial
- Is it safe to open an account by fraud
- 打新债100%中签的方法 开户是安全的吗
- centos7.3修改mysql默认密码_详解Centos7 修改mysql指定用户的密码
- Easy to use dictionary -defaultdict
- Performance rendering of dSPACE
- @PostConstruct
- 给你讲懂 MVCC 续篇
猜你喜欢

Refresh mechanism of vie

PyTorch学习笔记(七)------------------ Vision Transformer

Detailed explanation of cache (for the postgraduate entrance examination of XD)

Expressing the transformation of two coordinate systems with vectors

How transformers Roberta adds tokens

20年ICPC澳门站L - Random Permutation

软件测试周刊(第77期):只要放弃一次,就会滋生放弃的习性, 原本可以解决的问题也会变得无法解决。

Use xxl-job to customize tasks and schedule them

Distributed transaction solutions and code implementation

AI自己写代码让智能体进化!OpenAI的大模型有“人类思想”那味了
随机推荐
Overview of AOSP ~ WiFi architecture
Once beego failed to find bee after passing the go get command Exe's pit
PE file infrastructure sorting
Groovy之高级用法
C#实现水晶报表绑定数据并实现打印
Using qdomdocument to manipulate XML files in QT
商城项目 pc----商品详情页
Dirvish Chinese document of vim
Advanced usage of groovy
XML建模
Advanced mathematics | proficient in mean value theorem problem solving routines summary
使用ShaderGraph制作边缘融合粒子Shader的启示
mysql学习笔记--单张表上的增删改查
Modifying universal render data at runtime
NPM package publishing tutorial
Centos7.3 modifying MySQL default password_ Explain centos7 modifying the password of the specified user in MySQL
Expressing the transformation of two coordinate systems with vectors
打新债是不是骗局 开户是安全的吗
Refresh mechanism of vie
UnityShader入门精要——表面着色器