当前位置:网站首页>Structure - C language
Structure - C language
2022-07-05 14:21:00 【Cwxh0125】
Why use a structure
If the data you want to express is complex , It's not a value For example, you want to express the date There are three values of month, year and day , If you want to express so many data gathered together as a whole Then we need to use Structure
A structure is a composite data type
#include <stdio.h>
int main(int argc,char const *argv[])
{
struct date {
int month;
int day;
int year;
};
struct date toaay;
today. month = 07;
today.day = 31;
today .year = 2014;
printf( "Today's date is %i-%i-%i.\n",
today.year,today.month,today.day ) ;
return 0;
}
Within the function / Outside ?
And local variables — sample , The structure type declared inside the function can only be used inside the function
Therefore, the structure type is usually declared outside the function ,
In this way, it can be used by multiple functions
The form of the declaration structure
Three methods
struct point {
int x;
int y;};struct point pl, p2;pl and p2 All are point There are x and y Value
struct {
int x;
int y;} p1,p2 ;p1 and p2 It's all a nameless structure , There are x and y
struct point {
int x;
int y;} pl, p2;pl and p2 All are point There are x and y Value
Initialization of structure
#include <stdio.h>
struct date {
int month;
int day;
int year;
};
int main(int argc, char const *argv[])
{
struct date today = {07,31,2014}; \\ Assign values by
struct date thismonth = {.month=7, .year=2014}; \\ Assign assignment
printf("Today's date is %i-%i-%i.\n",
today. year,today.month,today.day ) ;
printf("This month is %i-%i-%i. ln",
thismonth.year,thismonth.month,thismonth.day ) ;
return 0;
}
Structural members
Structure and array are a bit like
Arrays use operators and subscripts to access their members a[0] =10;
For structure . Operators and names access their members
today.day visit today The structure of the day
p1.x visit p1 The structure of the x
Structural operations
To access the whole structure , Directly use the name of the structure variable for the whole structure , It can be assigned 、 Address fetch , It can also be passed to function parameters
pl = (struct point){5,I0}; amount to pl.x= 5; pl.y = l0;
pl =p2; amount to pl.x = p2.x; pl.y = p2.y;
Arrays can't do these two operations !
边栏推荐
- mysql 自定义函数 身份证号转年龄(支持15/18位身份证)
- Use the word "new" to attract curious people
- Postman简介、安装、入门使用方法详细攻略!
- 魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
- TiCDC 6.0原理之Sorter演进
- 世界环境日 | 周大福用心服务推动减碳环保
- How to introduce devsecops into enterprises?
- 判断变量是否为数组
- 01 、Solr7.3.1 在Win10平台下使用jetty的部署及配置
- Guofu hydrogen energy rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, and 360million yuan of accounts receivable exceed the revenue
猜你喜欢
Sharing the 12 most commonly used regular expressions can solve most of your problems
Shen Ziyu, nouveau Président de Meizu: M. Huang Zhang, fondateur de Meizu, agira comme conseiller stratégique pour les produits scientifiques et technologiques de Meizu
为什么我认识的机械工程师都抱怨工资低?
TDengine 社区问题双周精选 | 第三期
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
What are the advantages and characteristics of SAS interface
Thymeleaf th:with use of local variables
How to protect user privacy without password authentication?
Principle and performance analysis of lepton lossless compression
基于 TiDB 场景式技术架构过程 - 理论篇
随机推荐
The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
2022年国内正规的期货公司平台有哪些啊?方正中期怎么样?安全可靠吗?
最长公共子序列 - 动态规划
Linux下mysql数据库安装教程
Hongmeng fourth training
Tiflash compiler oriented automatic vectorization acceleration
TiCDC 6.0原理之Sorter演进
MySQL user-defined function ID number to age (supports 15 / 18 digit ID card)
R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
LeetCode_ 67 (binary sum)
SAS接口有什么优势特点
LeetCode_ 3 (longest substring without repeated characters)
Guofu hydrogen energy rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, and 360million yuan of accounts receivable exceed the revenue
LeetCode_ 2 (add two numbers)
PMP考试20天能通过吗?
tidb-dm报警DM_sync_process_exists_with_error排查
C - Divisors of the Divisors of An Integer Gym - 102040C
Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
Don't be unconvinced. Mobile phone function upgrade is strong