当前位置:网站首页>结构体初解
结构体初解
2022-08-05 03:19:00 【悄悄卷s所有人】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
以下内容是我在学习C语言过程中的经验总结,如果对你有所帮助,麻烦关注、点赞支持一下,蟹蟹!
提示:以下是本篇文章正文内容,下面案例可供参考
1. 结构体的声明
1.1 结构的基础知识
结构是一些值的集合,这些值称为成员变量。结构的每个成员可以是不同类型的变量。
1.2 结构的声明
struct 结构体名
{
成员列表;
}变量名;
注意:
1.变量名后边要记得加“ ;”
2.如果结构体是在main 函数前定义的,且在结构体声明类型的同时,在其后定义变量名,则变量名所表示的变量为全局变量,用的比较少;
一般将其在main 函数中定义,使其为局部变量。(C语言规定:尽量少的使用全局变量)
例如:
定义一个“人”:
全局变量型:
#include<stdio.h>
struct people
{
char name[20];//名字
int age;//年龄
char sex[3];//性别
int hight;//身高
}s1,s2,s3,s4;//在结构体声明类型的同时,在其后定义变量名,则变量名所表示的变量为全局变量
int main()
{
return 0;
}
//也可以声明为:
#include<stdio.h>
struct people s1,s2,s3,s4;//全局变量
int main()
{
return 0;
}
//people 是结构体标签名;
//char name[20];int age;char sex[3];int hight; 等就是成员列表;
//s1,s2,s3,s4 就是变量名
局部变量型:
#include<stdio.h>
int main()
{
struct people s1,s2,s3,s4;//在主函数中定义变量,使其为局部变量
return 0;
}
1.3 结构体成员的类型
结构体成员可以是标量、数组、指针、甚至是其它结构体。
1.4 结构体变量的定义和初始化
1.结构体的初始化用“ {} ”
例1:
#include<stdio.h>
struct Point
{
int x;
int y;
};
struct stu
{
char name[20];//名字
int age;//年龄
char sex[3];//性别
int hight;//身高
};
int main()
{
struct Point p = {
10,20 };//结构体变量的定义和初始化,因为下x、y都为int 型,所以将其初始化为10、20两个整形
struct stu s = {
"zhangsan",20,"男",180 };
printf("x=%d y=%d\n", p.x, p.y);
printf("%s %d %s %d\n", s.name, s.age, s.sex, s.hight);
return 0;
}
例2:
结构体中包含结构体:
#include<stdio.h>
struct Point
{
int x;
int y;
};
struct stu
{
char name[20];//名字
int age;//年龄
char sex[3];//性别
int hight;//身高
};
struct S
{
char c;
struct Point p;//结构体
double d;
char str[20];
};
int main()
{
struct S w = {
'x',{
100,200},3.14,"haha" };//在结构体中初始化另外一个结构体时,需要在结构体的“{ }”中再包含一个“{ }”
//也可以: struct S w = { .d = 3.14 , .p = { 100 , 200 } , .c = 'x' , .str = "haha" }; 这种用的比较少 一般用于不完全初始化,如:只初始化 .d 或 .c 等
struct Point p = {
10,20 };//结构体变量的定义和初始化,因为下x、y都为int 型,所以将其初始化为10、20两个整形
struct stu s = {
"zhangsan",20,"男",180 };
printf("x=%d y=%d\n", p.x, p.y);
printf("%s %d %s %d\n", s.name, s.age, s.sex, s.hight);
printf("%c %d %d %lf %s\n", w.c, w.p, w.d, w.str);
return 0;
}
注意:
当想要更改name 中的内容时,因为name 的类型是字符型数组,其数组名(即结构体中的变量名)只是数组首元素的地址,所以不能直接更改,需要使用 strcpy(拷贝)来更改字符串的内容。如:
s.name = " zhangsanfeng " ; //这是一种错误的写法,正确写法是: strcpy( s.name , "zhangsanfeng ");
2. 结构体成员的访问及结构体传参
结构体变量 . 结构体成员名
结构体指针 -> 结构体成员名
2.1 结构体变量访问成员
1. 结构体变量的成员时通过点操作符( . )访问的。点操作符接受两个操作数。
例:函数传参
传值调用:
形参是实参的一份临时拷贝
#include<stdio.h>
struct S
{
int a[100];
char b[10];
};
void print(struct S ww)//传参时,传过来的参数用一个结构体来接收
{
int i=0;
for(i=0; i <10 ;i++)
{
printf("%d " , ww.a[i]);
}
printf("\n %s\n" ,ww.b );
}
int main()
{
struct S w = {
{
1,2,3},"haha"};//未完全初始化
print(w);
return 0;
}
2. 结构体变量的成员时通过箭头作符( ->)访问的。
例:
传址调用:
#include<stdio.h>
struct S
{
int a[100];
char b[10];
};
void print(struct S* ww)
{
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", ww -> a[i]);
}
printf("\n%s\n", ww -> b);
}
int main()
{
struct S w = {
{
1,2,3},"haha" };
print(&w);
return 0;
}
2.2 结构体传参
传址调用:(使用较多)
实参传过去的是地址,形参占用的内存小,所以使用较多。
传值调用:(使用较少)
形参是实参的一份临时拷贝,空间上占用的内存大,容易造成浪费,所以使用较少。
总结
以上内容为博主的一些经验总结,如内容有误请留言指正。
边栏推荐
- Programmer's Tanabata Romantic Moment
- Physical backup issues caused by soft links
- High Item 02 Information System Project Management Fundamentals
- 21 Days Learning Challenge (2) Use of Graphical Device Trees
- Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors
- J9 Digital Currency: What is the creator economy of web3?
- How to sort multiple fields and multiple values in sql statement
- STM32 uses stm32cubemx LL library series tutorial
- The problem of lack of dynamic library "libtinfo.so.5" in ksql application under UOS system
- undo problem
猜你喜欢
倒计时 2 天|云原生 Meetup 广州站,等你来!
Apache DolphinScheduler, a new generation of distributed workflow task scheduling platform in practice - Medium
毕设-基于SSM房屋租赁管理系统
【软件测试】自动化测试之unittest框架
Beyond YOLO5-Face | YOLO-FaceV2 officially open source Trick+ academic point full
沃谈小知识 |“远程透传”那点事儿
How to Add Category-Specific Widgets in WordPress
Why is the pca component not associated
金仓数据库如何验证安装文件平台正确性
21 Days Learning Challenge (2) Use of Graphical Device Trees
随机推荐
Lexicon - the maximum depth of a binary tree
After the large pixel panorama is completed, what are the promotion methods?
2022-08-04 第六小组 瞒春 学习笔记
word分栏小记
腾讯云【Hiflow】新时代自动化工具
今年七夕,「情蔬」比礼物更有爱
【已解决】Unity Coroutinue 协程未有效执行的问题
Syntax basics (variables, input and output, expressions and sequential statements)
ASP.NET application--Hello World
Data storage practice based on left-order traversal
如何在WordPress中添加特定类别的小工具
YYGH-13-客服中心
倒计时 2 天|云原生 Meetup 广州站,等你来!
(十一)元类
Ant Sword Advanced Module Development
Object.defineProperty monitors data changes in real time and updates the page
mysql can't Execute, please solve it
用CH341A烧录外挂Flash (W25Q16JV)
Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕