首页 > 后端开发 > C++ > 正文

编写一个C程序来显示结构成员的大小和偏移量

WBOY
发布: 2023-08-29 20:09:19
转载
719人浏览过

编写一个c程序来显示结构成员的大小和偏移量

问题

编写一个C程序来定义结构体并显示成员变量的大小和偏移量

结构体 - 它是一个不同数据类型变量的集合,组合在一个名称下。

结构声明的一般形式

datatype member1;
struct tagname{
   datatype member2;
   datatype member n;
};
登录后复制

在这里,struct  - 关键字

tagname - 指定结构的名称

member1, member2 - 指定构成结构的数据项。

示例

struct book{
   int pages;
   char author [30];
   float price;
};
登录后复制

结构体变量

声明结构体变量有三种方式 -

方法一

struct book{
   int pages;
   char author[30];
   float price;
}b;
登录后复制

方法2

struct{
   int pages;
   char author[30];
   float price;
}b;
登录后复制

方法三

struct book{
   int pages;
   char author[30];
   float price;
};
struct book b;
登录后复制

初始化和访问结构

成员与结构变量之间的链接是通过成员运算符(或者点运算符)建立的。

可以通过以下方式进行初始化:

方法1

struct book{
   int pages;
   char author[30];
   float price;
} b = {100, "balu", 325.75};
登录后复制

方法2

struct book{
   int pages;
   char author[30];
   float price;
};
struct book b = {100, "balu", 325.75};
登录后复制

方法3(使用成员运算符)

struct book{
   int pages;
   char author[30];
   float price;
} ;
struct book b;
b. pages = 100;
strcpy (b.author, "balu");
b.price = 325.75;
登录后复制

方法四(使用scanf函数)

struct book{
   int pages;
   char author[30];
   float price;
} ;
struct book b;
   scanf ("%d", &b.pages);
   scanf ("%s", b.author);
   scanf ("%f", &b. price);
登录后复制

使用数据成员声明结构,并尝试打印它们的偏移值以及结构的大小。

程序

 实时演示

#include<stdio.h>
#include<stddef.h>
struct tutorial{
   int a;
   int b;
   char c[4];
   float d;
   double e;
};
int main(){
   struct tutorial t1;
   printf("the size 'a' is :%d</p><p>",sizeof(t1.a));
   printf("the size 'b' is :%d</p><p>",sizeof(t1.b));
   printf("the size 'c' is :%d</p><p>",sizeof(t1.c));
   printf("the size 'd' is :%d</p><p>",sizeof(t1.d));
   printf("the size 'e' is :%d</p><p>",sizeof(t1.e));
   printf("the offset 'a' is :%d</p><p>",offsetof(struct tutorial,a));
   printf("the offset 'b' is :%d</p><p>",offsetof(struct tutorial,b));
   printf("the offset 'c' is :%d</p><p>",offsetof(struct tutorial,c));
   printf("the offset 'd' is :%d</p><p>",offsetof(struct tutorial,d));
   printf("the offset 'e' is :%d</p><p></p><p>",offsetof(struct tutorial,e));
   printf("size of the structure tutorial is :%d",sizeof(t1));
   return 0;
}
登录后复制

输出

the size 'a' is :4
the size 'b' is :4
the size 'c' is :4
the size 'd' is :4
the size 'e' is :8
the offset 'a' is :0
the offset 'b' is :4
the offset 'c' is :8
the offset 'd' is :12
the offset 'e' is :16

size of the structure tutorial is :24
登录后复制

以上就是编写一个C程序来显示结构成员的大小和偏移量的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号