当前位置:网站首页>结构体初解
结构体初解
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 结构体传参
传址调用:(使用较多)
实参传过去的是地址,形参占用的内存小,所以使用较多。
传值调用:(使用较少)
形参是实参的一份临时拷贝,空间上占用的内存大,容易造成浪费,所以使用较少。
总结
以上内容为博主的一些经验总结,如内容有误请留言指正。

边栏推荐
- Step by step how to perform data risk assessment
- 如何在WordPress中添加特定类别的小工具
- Multithreading (2)
- Beyond YOLO5-Face | YOLO-FaceV2 officially open source Trick+ academic point full
- 语法基础(变量、输入输出、表达式与顺序语句)
- 今年七夕,「情蔬」比礼物更有爱
- tree table lookup
- 龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
- 2022-08-04 The sixth group, hidden from spring, study notes
- How to simulate the background API call scene, very detailed!
猜你喜欢

Talking about data security governance and privacy computing

2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试

.NET Application -- Helloworld (C#)

【软件测试】自动化测试之unittest框架

2022 High-level installation, maintenance, and removal of exam questions mock exam question bank and online mock exam
![[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)](/img/10/dafea90158adf9d43c4f025414fef7.png)
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)

北斗三号短报文终端露天矿山高边坡监测方案

冰蝎V4.0攻击来袭,安全狗产品可全面检测

Bubble Sort and Quick Sort

How OpenGL works
随机推荐
Common open source databases under Linux, how many do you know?
Use @Mapper to query the partition status of oracle and report an error
Solve the problem of port occupancy Port xxxx was already in use
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)
基于生长的棋盘格角点检测方法
Why is the pca component not associated
[Solved] Unity Coroutine coroutine is not executed effectively
思考(八十八):使用 protobuf 自定义选项,做数据多版本管理
Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!
Linux下常见的开源数据库,你知道几个?
On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion
CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
burp安装及代理设置
sql server 安装提示用户名不存在
Programmer's Tanabata Romantic Moment
达梦8数据库导出导入
Open Source License Description LGPL
高项 02 信息系统项目管理基础
(十一)元类
Syntax basics (variables, input and output, expressions and sequential statement completion)