当前位置:网站首页>Initial solution of the structure
Initial solution of the structure
2022-08-05 03:24:00 【Quietly roll s everyone】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
以下内容是我在学习CSummary of experience in language process,如果对你有所帮助,麻烦关注、点赞支持一下,蟹蟹!
提示:以下是本篇文章正文内容,下面案例可供参考
1. 结构体的声明
1.1 结构的基础知识
结构是一些值的集合,这些值称为成员变量.结构的每个成员可以是不同类型的变量.
1.2 结构的声明
struct 结构体名
{
成员列表;
}变量名;
注意:
1.Remember to add it after the variable name“ ;”
2.if the struct is inmain defined before the function,And at the same time as the struct declares the type,Define the variable name after it,The variable represented by the variable name is a global variable,用的比较少;
Generally leave it theremain 函数中定义,Make it a local variable.(C语言规定:尽量少的使用全局变量)
例如:
定义一个“人”:
Global variable type:
#include<stdio.h>
struct people
{
char name[20];//名字
int age;//年龄
char sex[3];//性别
int hight;//身高
}s1,s2,s3,s4;//while the struct declares the type,Define the variable name after it,The variable represented by the variable name is a global variable
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; etc. is the list of members;
//s1,s2,s3,s4 就是变量名
local variable type:
#include<stdio.h>
int main()
{
struct people s1,s2,s3,s4;//在主函数中定义变量,Make it a local variable
return 0;
}
1.3 结构体成员的类型
结构体成员可以是标量、数组、指针、甚至是other structures.
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 型,So initialize it as 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" };//When initializing another struct in a struct,Need to be in the structure“{ }”中再包含一个“{ }”
//也可以: struct S w = { .d = 3.14 , .p = { 100 , 200 } , .c = 'x' , .str = "haha" }; 这种用的比较少 Typically used for incomplete initialization,如:只初始化 .d 或 .c 等
struct Point p = {
10,20 };//结构体变量的定义和初始化,因为下x、y都为int 型,So initialize it as 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 The type is a character array,其数组名(That is, the variable name in the structure)Just the address of the first element of the array,所以不能直接更改,需要使用 strcpy(拷贝)to change the content of the string.如:
s.name = " zhangsanfeng " ; //这是一种错误的写法,正确写法是: strcpy( s.name , "zhangsanfeng ");
2. Structure member access and structure parameter transfer
结构体变量 . 结构体成员名
结构体指针 -> 结构体成员名
2.1 结构体变量访问成员
1. A member of a structure variable is passed the dot operator( . )访问的.点操作符接受两个操作数.
例:函数传参
传值调用:
形参是实参的一份临时拷贝
#include<stdio.h>
struct S
{
int a[100];
char b[10];
};
void print(struct S ww)//传参时,The passed parameters are received by a structure
{
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. The member of the structure variable is passed the arrow operator( ->)访问的.
例:
传址调用:
#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 结构体传参
传址调用:(使用较多)
实参传过去的是地址,Formal parameters take up little memory,So use more.
传值调用:(使用较少)
形参是实参的一份临时拷贝,It takes up a lot of memory space,容易造成浪费,所以使用较少.
总结
The above content is a summary of some of the experience of bloggers,If the content is wrong, please leave a message to correct.
边栏推荐
- public static
List asList(T... a) What is the prototype? - presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
- Why is the pca component not associated
- Never put off till tomorrow what you can put - house lease management system based on the SSM
- 调用阿里云oss和sms服务
- Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors
- 引领数字医学高地,中山医院探索打造未来医院“新范式”
- 龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
- undo problem
- public static <T> List<T> asList(T... a) 原型是怎么回事?
猜你喜欢
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)
论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕
On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion
Flink 1.15.1 Cluster Construction (StandaloneSession)
.NET应用程序--Helloworld(C#)
用Unity发布APP到Hololens2无坑教程
Question about #sql shell#, how to solve it?
.NET Application -- Helloworld (C#)
Use SuperMap iDesktopX data migration tool to migrate map documents and symbols
Developing Hololens encountered The type or namespace name 'HandMeshVertex' could not be found..
随机推荐
1484. Sell Products by Date
QT language file production
【已解决】Unity Coroutinue 协程未有效执行的问题
龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
Syntax basics (variables, input and output, expressions and sequential statements)
dmp (dump) dump file
毕设-基于SSM房屋租赁管理系统
How to transfer a single node of Youxuan database to a cluster
YYGH-13-Customer Service Center
2022.8.4-----leetcode.1403
Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
Principle and Technology of Virtual Memory
ASP.NET应用程序--Hello World
sql怎么找字段里所有数据为空的字段
语法基础(变量、输入输出、表达式与顺序语句完成情况)
burp安装及代理设置
J9 Digital Currency: What is the creator economy of web3?
In 2022, you still can't "low code"?Data science can also play with Low-Code!
Burp installation and proxy settings