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

边栏推荐
- 21 Days Learning Challenge (2) Use of Graphical Device Trees
- Native js realizes the effect of selecting and canceling all the multi-select boxes
- Common open source databases under Linux, how many do you know?
- 基于生长的棋盘格角点检测方法
- 调用阿里云oss和sms服务
- The linear table lookup
- 【已解决】Unity Coroutinue 协程未有效执行的问题
- Details such as compiling pretreatment
- Tencent Cloud [Hiflow] New Era Automation Tool
- Distributed systems revisited: there will never be a perfect consistency scheme...
猜你喜欢

【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)

Cloud Native (32) | Introduction to Platform Storage System in Kubernetes

word分栏小记

Multithreading (2)

Why did they choose to fall in love with AI?

腾讯云【Hiflow】新时代自动化工具

In 2022, you still can't "low code"?Data science can also play with Low-Code!

Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme

On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion

IJCAI2022 | DictBert: Pre-trained Language Models with Contrastive Learning for Dictionary Description Knowledge Augmentation
随机推荐
Everyone in China said data, you need to focus on core characteristic is what?
One hundred - day plan -- -- DAY2 brush
The usage of try...catch and finally in js
ASP.NET应用程序--Hello World
Programmer's Tanabata Romantic Moment
MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
mysql没法Execute 大拿们求解
毕设-基于SSM房屋租赁管理系统
From "useable" to "easy to use", domestic software is self-controllable and continues to advance
[论文笔记] MapReduce: Simplified Data Processing on Large Clusters
word column notes
惨遭打脸:字节某部门竟有这么多测试员
【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)
undo problem
论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕
In 2022, you still can't "low code"?Data science can also play with Low-Code!
Syntax basics (variables, input and output, expressions and sequential statement completion)
Syntax basics (variables, input and output, expressions and sequential statements)
The Tanabata copywriting you want has been sorted out for you!
How to solve the error cannot update secondary snapshot during a parallel operation when the PostgreSQL database uses navicat to open the table structure?