当前位置:网站首页>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.
边栏推荐
- Is your data safe in this hyperconnected world?
- UOS系统下ksql应用缺少动态库”libtinfo.so.5“问题
- Distributed systems revisited: there will never be a perfect consistency scheme...
- 2022 High-level installation, maintenance, and removal of exam questions mock exam question bank and online mock exam
- Open-Falcon of operation and maintenance monitoring system
- 21天学习挑战赛(2)图解设备树的使用
- 1484. Sell Products by Date
- Thinking (88): Use protobuf custom options for multi-version management of data
- YYGH-13-Customer Service Center
- rpc-remote procedure call demo
猜你喜欢
Simple description of linked list and simple implementation of code
.NET Application -- Helloworld (C#)
【已解决】Unity Coroutinue 协程未有效执行的问题
The linear table lookup
2022-08-04 The sixth group, hidden from spring, study notes
21 Days Learning Challenge (2) Use of Graphical Device Trees
Web3.0 Dapps——通往未来金融世界的道路
word column notes
2022 High-level installation, maintenance, and removal of exam questions mock exam question bank and online mock exam
【软件测试】自动化测试之unittest框架
随机推荐
Native js realizes the effect of selecting and canceling all the multi-select boxes
In 2022, you still can't "low code"?Data science can also play with Low-Code!
leetcode - a subtree of another tree
【 genius_platform software platform development 】 : seventy-six vs the preprocessor definitions written cow force!!!!!!!!!!(in the other groups conding personnel told so cow force configuration to can
A small tool to transfer files using QR code - QFileTrans 1.2.0.1
引领数字医学高地,中山医院探索打造未来医院“新范式”
burp安装及代理设置
语法基础(变量、输入输出、表达式与顺序语句)
静态方法获取配置文件数据
开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
private封装
ASP.NET应用程序--Hello World
tree table lookup
(11) Metaclass
ffmpeg 像素格式基础知识
.NET应用程序--Helloworld(C#)
Call Alibaba Cloud oss and sms services
高项 02 信息系统项目管理基础
word column notes
ffmpeg enumeration decoders, encoders analysis