二维码
微世推网

扫一扫关注

当前位置: 首页 » 快报资讯 » 今日快报 » 正文

C语言二级必备知识_C语言结构体基本介绍。

放大字体  缩小字体 发布日期:2021-12-26 11:30:45    作者:田芮菡    浏览次数:279
导读

结构体是一种构造数据类型。用于把不同类型数据合并成为一个整体定义:struct结构体名 {类型标识符成员名;类型标识符 成员名;// 结构体类型定义得时候,//不可以赋初值。}; // 不要忘记结尾得分号结构体得声明:structstu { int num;char name [30];};stu是结构体类型,类型不分配内存,不能赋值,不能运算,不能存取。s

结构体是一种构造数据类型。用于把不同类型数据合并成为一个整体定义:

struct 结构体名 { 类型标识符 成员名; 类型标识符 成员名; // 结构体类型定义得时候, //不可以赋初值。 }; // 不要忘记结尾得分号

结构体得声明:

struct stu { int num; char name [30]; };

stu是结构体类型,类型不分配内存,不能赋值,不能运算,不能存取。

struct stu { int num ; char name[30];} st1;

st1 是结构体变量分配内存,可以赋值,存储。

结构体变量定义得两种形式:

// 第壹种: 直接在类型后面定义struct stu{ int num; char name [20];} st1; // 不要忘记后面得分号 // 第二种形式: void main () { struct stu st1 ; // 不要忘记声明类型 }

初始化结构体变量得三种形式:

//第壹种形式: struct stu { int num; char name[20] }st1={ 100 , "sda"}; // 第二种: struct stu st1={1001,"sda"}; //第三种: st1.num=100; st1.name="sdda"; // 错误 sprintf (st1.name,sdda"); // 因为字符串是常量,不能被直接赋值

结构体用大括号赋值,只有在创建并初始化得时候才可以。

定义匿名结构体变量: 唯一得方法:

struct { int num; char name [30];} st1;

结构体变量得引用:

结构体变量名 . 成员

可以将一个结构体变量赋值给另一个结构体变量。但是两个结构体变量类型必须是同一类型

struct stu { int num; char name[20]; struct chid { int age; char sex; };};//结构体内部再次定义一个结构体,但是没有定义实例得时候,//再次定义得结构体内部得变量,会被当作母体结构体变量成员。

struct stu { int num; char name[20]; struct chid { int age; char sex; } hha={....};}st1;

结构体数组:

定义:struct student{ int num; char name[];};struct student stu[] ={ {100,"aaa"}, {101,"asd"}, {102,"asd"} }; struct student stu[] ={ 100,"s",102,"sa",103,"sad"}; // 也可以挨个得赋值。 但是匿名结构体不可以挨个得赋值。

结构体指针:

一,定义: struct 结构体名 *结构体指针名 (:存放结构体变量得起始地址) 指向结构体变量数组得指针: struct students { int num; char name[20]; }; struct students stu[3] ={ {100,"aaa"}, {101,"asd"}, {102,"asd"} }; struct students stu *p= stu; 二,引用: 1. 用成员名引用: stu[0].num; stu[i].name 2. 指针引用: *p.num ; *(p+1) .name p->num ; (P+1)-> name 3. 指针循环: for (;p<stu+n;p++) { p->num; p->name; }

 
(文/田芮菡)
免责声明
• 
本文仅代表发布者:田芮菡个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,需自行承担相应责任。涉及到版权或其他问题,请及时联系我们删除处理邮件:weilaitui@qq.com。
 

Copyright©2015-2025 粤公网安备 44030702000869号

粤ICP备16078936号

微信

关注
微信

微信二维码

WAP二维码

客服

联系
客服

联系客服:

24在线QQ: 770665880

客服电话: 020-82301567

E_mail邮箱: weilaitui@qq.com

微信公众号: weishitui

韩瑞 小英 张泽

工作时间:

周一至周五: 08:00 - 24:00

反馈

用户
反馈