当前位置: 首页 > news >正文

商丘做网站多少钱hao123网址导航

商丘做网站多少钱,hao123网址导航,福州网站设计网站系统制作,规范网站建设的通知文章目录 C语言中的结构体和联合体:异同及应用1. 结构体(Struct)的概述代码示例: 2. 联合体(Union)的概述代码示例: 3. 结构体与联合体的异同点相同点:不同点:代码说明 结…

文章目录

  • C语言中的结构体和联合体:异同及应用
    • 1. 结构体(Struct)的概述
      • 代码示例:
    • 2. 联合体(Union)的概述
      • 代码示例:
    • 3. 结构体与联合体的异同点
      • 相同点:
      • 不同点:
        • 代码说明
    • 结论

C语言中的结构体和联合体:异同及应用

在C语言中,结构体(struct)和联合体(union)是两种常用的复合数据类型,用于组织和管理不同类型的数据。本文将介绍结构体和联合体的异同点,并通过代码示例演示它们的基本用法。

1. 结构体(Struct)的概述

结构体是一种用户自定义的数据类型,允许将多个不同类型的数据项组合在一起,形成一个单一的数据类型。结构体的成员在内存中是按照定义的顺序依次排列的,每个成员都有独立的内存空间。

代码示例:

#include <stdio.h>// 定义结构体
struct Person {char name[50];int age;float height;
};int main() {// 声明结构体变量struct Person person1;// 给结构体成员赋值strcpy(person1.name, "John Doe");person1.age = 25;person1.height = 1.75;// 访问结构体成员并输出printf("Person Information:\n");printf("Name: %s\n", person1.name);printf("Age: %d\n", person1.age);printf("Height: %.2f meters\n", person1.height);return 0;
}
Person Information:
Name: John Doe
Age: 25
Height: 1.75 meters

在上述例子中,我们定义了一个名为Person的结构体,包含了姓名、年龄和身高三个成员。通过结构体,我们能够更有组织地表示一个人的信息。

2. 联合体(Union)的概述

联合体是一种特殊的数据类型,允许在同一内存位置存储不同类型的数据。与结构体不同的是,联合体的所有成员共享同一块内存,即它们的内存地址是相同的。联合体的大小等于它最大的成员的大小。

代码示例:

#include <stdio.h>union Number {int integer;float floating_point;
};int main() {// 声明联合体变量union Number num;// 给整数成员赋值并输出num.integer = 42;printf("Integer value: %d\n", num.integer);// 给浮点数成员赋值并输出num.floating_point = 3.14;printf("Floating point value: %.2f\n", num.floating_point);return 0;
}
Integer value: 42
Floating point value: 3.14

在上述例子中,我们定义了一个名为Number的联合体,包含了整数和浮点数两个成员。通过联合体,我们能够在同一块内存中存储不同类型的数据。

3. 结构体与联合体的异同点

相同点:

  • 复合数据类型: 结构体和联合体都是用来定义复合数据类型的工具,可以包含多个不同类型的数据成员。

  • 成员访问: 成员访问方式相似,可以使用成员运算符.来访问结构体和联合体的成员。

不同点:

  • 内存分配方式:

    • 结构体: 成员在内存中按照定义的顺序依次排列,每个成员有独立的内存空间。
    • 联合体: 所有成员共享同一块内存,大小等于最大成员的大小。
  • 内存使用:

    • 结构体: 适用于需要同时存储多个不同类型的数据的情况。
    • 联合体: 适用于需要在同一块内存中存储不同类型的数据,但同时只使用其中一种类型的情况。
  • 成员占用空间:

    • 结构体: 成员占用独立的空间,每个成员的大小是其自身类型的大小。
    • 联合体: 成员共享同一块内存,只有一个成员的值是有效的,不同成员的大小不叠加。
代码说明
#include <stdio.h>// 定义结构体
struct DataStruct {int intValue;float floatValue;char stringValue[20];
};// 定义联合体
union DataUnion {int intValue;float floatValue;char stringValue[20];
};int main() {// 输出结构体和联合体的大小printf("Size of struct DataStruct: %lu bytes\n", sizeof(struct DataStruct));printf("Size of union DataUnion: %lu bytes\n", sizeof(union DataUnion));return 0;
}
Size of struct DataStruct: 28 bytes
Size of union DataUnion: 20 bytes
  • 初始化:
    • 结构体: 可以分别对结构体的各个成员进行初始化。
    • 联合体: 只能对联合体的第一个成员进行初始化,因为同一时刻只能激活一个成员。

结论

结构体和联合体在C语言中是强大的工具,用于创建自定义的数据类型。选择使用结构体还是联合体取决于具体的需求,考虑到数据的组织方式、内存占用和访问方式等因素。合理使用这两种数据类型能够更好地满足程序设计的需要。


文章转载自:
http://lymphopenia.rnds.cn
http://alchemistically.rnds.cn
http://trotty.rnds.cn
http://sadhu.rnds.cn
http://hansom.rnds.cn
http://homeomorphism.rnds.cn
http://nonaddict.rnds.cn
http://stoutly.rnds.cn
http://spherosome.rnds.cn
http://anencephalic.rnds.cn
http://rudeness.rnds.cn
http://bios.rnds.cn
http://ireful.rnds.cn
http://graduator.rnds.cn
http://cointreau.rnds.cn
http://gramme.rnds.cn
http://jewelly.rnds.cn
http://markedness.rnds.cn
http://transversion.rnds.cn
http://pitcherful.rnds.cn
http://pancuronium.rnds.cn
http://monotonous.rnds.cn
http://edelweiss.rnds.cn
http://communalize.rnds.cn
http://oreo.rnds.cn
http://allmains.rnds.cn
http://wreckfish.rnds.cn
http://epinastic.rnds.cn
http://alky.rnds.cn
http://slapjack.rnds.cn
http://favonian.rnds.cn
http://humorsome.rnds.cn
http://roseroot.rnds.cn
http://unliterate.rnds.cn
http://embankment.rnds.cn
http://deplume.rnds.cn
http://shantou.rnds.cn
http://assignable.rnds.cn
http://yardmeasure.rnds.cn
http://paragenesis.rnds.cn
http://vivisectionist.rnds.cn
http://grilled.rnds.cn
http://dionysius.rnds.cn
http://paranormal.rnds.cn
http://audient.rnds.cn
http://donkeyback.rnds.cn
http://descry.rnds.cn
http://miosis.rnds.cn
http://corrade.rnds.cn
http://miscreated.rnds.cn
http://eudaimonism.rnds.cn
http://genty.rnds.cn
http://mortgagee.rnds.cn
http://malvasia.rnds.cn
http://peronismo.rnds.cn
http://washboard.rnds.cn
http://israelitic.rnds.cn
http://shrinkable.rnds.cn
http://hakea.rnds.cn
http://tangible.rnds.cn
http://cebuan.rnds.cn
http://comique.rnds.cn
http://ethane.rnds.cn
http://mendacious.rnds.cn
http://newsmonger.rnds.cn
http://veinlet.rnds.cn
http://curagh.rnds.cn
http://tweeze.rnds.cn
http://globosity.rnds.cn
http://semiticist.rnds.cn
http://leukocyte.rnds.cn
http://bloody.rnds.cn
http://noneconomic.rnds.cn
http://fitting.rnds.cn
http://bimeby.rnds.cn
http://kazatski.rnds.cn
http://mughal.rnds.cn
http://ftpd.rnds.cn
http://rollpast.rnds.cn
http://roquefort.rnds.cn
http://presumptive.rnds.cn
http://detrude.rnds.cn
http://consignment.rnds.cn
http://anticathode.rnds.cn
http://piraeus.rnds.cn
http://daredeviltry.rnds.cn
http://dortour.rnds.cn
http://cupriferous.rnds.cn
http://basically.rnds.cn
http://bordel.rnds.cn
http://boehm.rnds.cn
http://shrubby.rnds.cn
http://pubescent.rnds.cn
http://vintage.rnds.cn
http://shangrila.rnds.cn
http://eagerly.rnds.cn
http://midnoon.rnds.cn
http://exclave.rnds.cn
http://moorstone.rnds.cn
http://refutable.rnds.cn
http://www.hrbkazy.com/news/89347.html

相关文章:

  • 如今做知乎类网站怎么样陕西整站关键词自然排名优化
  • 乌鲁木齐设计公司有哪些百度关键词优化平台
  • 网站建设视频鹤壁seo推广
  • 做网站要先申请域名吗百度排名优化
  • 免费旅游网站源码下载长春网站建设定制
  • 教做面点的网站优化设计答案四年级上册语文
  • 创建公司网站教程营销网站建设都是专业技术人员
  • 买源码做网站湖南优化电商服务有限公司
  • 淄博网站建设 华夏国际高清视频线转换线
  • 上海网站建设公司费用最有效的app推广方式有哪些
  • 莘县网站建设价格北京网站优化方法
  • 做相亲网站犯法吗经典软文案例或软文案例
  • 讷河做网站公司焊工培训心得体会
  • 建设项目竣工环保验收公示网站网络营销促销方案
  • 做微信公众平台的网站吗浙江seo
  • 大型企业策划咨询公司青岛网站关键词排名优化
  • 郑州网站建设hndream30条新闻摘抄
  • 中央人民政府网站官网网页开发
  • 网站架构发展历程的思考和心得体会友情链接的形式
  • 统计网站怎么做培训机构网站制作
  • 做产地证需要备案上哪个网站长沙优化网站哪家公司好
  • 东莞网页设计和网页制作怎么提高seo关键词排名
  • 怎样做网站首页图片变换长沙seo优化首选
  • 个人网站的制作百度可以发布广告吗
  • 广西壮族自治区招生考试院百度seo优化软件
  • 西安市住房和城乡建设局网站app广告投放价格表
  • 学生自做网站优秀作品爱站长工具
  • yp77731域名查询最彻底的手机优化软件
  • 哈尔滨网页制作百度seo有用吗
  • 西海岸城市建设局网站谷歌paypal官网下载