当前位置:网站首页>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.

边栏推荐
- Review 51 MCU
- Solve the problem of port occupancy Port xxxx was already in use
- Use SuperMap iDesktopX data migration tool to migrate ArcGIS data
- Matlab drawing 3
- word column notes
- Burp installation and proxy settings
- 冰蝎V4.0攻击来袭,安全狗产品可全面检测
- Distributed systems revisited: there will never be a perfect consistency scheme...
- Why is the pca component not associated
- Dameng 8 database export and import
猜你喜欢

Dynamic management of massive service instances

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

A small tool to transfer files using QR code - QFileTrans 1.2.0.1

2022-08-04 The sixth group, hidden from spring, study notes

运维监控系统之Open-Falcon

你要的七夕文案,已为您整理好!

The linear table lookup

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

IJCAI2022 | DictBert: Pre-trained Language Models with Contrastive Learning for Dictionary Description Knowledge Augmentation

Matlab drawing 3
随机推荐
Queue Topic: Recent Requests
The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined
为什么pca分量没有关联
语法基础(变量、输入输出、表达式与顺序语句完成情况)
In 2022, you still can't "low code"?Data science can also play with Low-Code!
AI+PROTAC | dx/tx completes $5 million seed round
Physical backup issues caused by soft links
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)
Study Notes-----Left-biased Tree
Open Source License Description LGPL
用Unity发布APP到Hololens2无坑教程
人人都在说的数据中台,你需要关注的核心特点是什么?
2022.8.4-----leetcode.1403
CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
Web3.0 Dapps——通往未来金融世界的道路
sql怎么找字段里所有数据为空的字段
How to find all fields with empty data in sql
Syntax basics (variables, input and output, expressions and sequential statements)
AI + Small Nucleic Acid Drugs | Eleven Completes $22 Million Seed Round Financing
J9 Digital Currency: What is the creator economy of web3?