当前位置:网站首页>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 !
边栏推荐
- 魅族新任董事长沈子瑜:创始人黄章先生将作为魅族科技产品战略顾问
- Matrix chain multiplication dynamic programming example
- 03_Solr之dataimport
- R语言使用ggplot2包的geom_histogram函数可视化直方图(histogram plot)
- 如何深入理解“有限状态机”的设计思想?
- ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
- mysql 自定义函数 身份证号转年龄(支持15/18位身份证)
- 为什么我认识的机械工程师都抱怨工资低?
- 微服务项目部署后,无法访问静态资源,无法访问到上传到upload中的文件,解决办法
- Scenario based technology architecture process based on tidb - Theory
猜你喜欢

网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理

Loop invariant

Kunlun Taike rushes to the scientific innovation board: the annual revenue is 130million, and it plans to raise 500million. CETC Taiji holds 40% of the shares

基于 TiDB 场景式技术架构过程 - 理论篇
![Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]](/img/c2/a5f5fe17a6bd1f6f9df828ddd224d6.png)
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]

How to deeply understand the design idea of "finite state machine"?

Principle and performance analysis of lepton lossless compression

家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级

Redis如何实现多可用区?

Tiflash compiler oriented automatic vectorization acceleration
随机推荐
Thymeleaf 模板的创建与使用
最长公共子序列 - 动态规划
C - Divisors of the Divisors of An Integer Gym - 102040C
R语言使用原生包(基础导入包、graphics)中的boxplot函数可视化箱图(box plot)
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
直播预告|如何借助自动化工具落地DevOps(文末福利)
Redis如何实现多可用区?
R language dplyr package select function, group_ By function, mutate function and cumsum function calculate the cumulative value of the specified numerical variable in the dataframe grouping data and
Google eventbus usage details
TDengine 社区问题双周精选 | 第三期
Hongmeng fourth training
让秒杀狂欢更从容:大促背后的数据库(下篇)
Matrix chain multiplication dynamic programming example
tidb-dm报警DM_sync_process_exists_with_error排查
R language ggplot2 visual bar graph: visualize the bar graph through the two-color gradient color theme, and add label text for each bar (geom_text function)
【学习笔记】阶段测试1
LeetCode_ 2 (add two numbers)
Time to calculate cron expression based on cronsequencegenerator
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and use the labs parameter to customize the X axis label text (customize X axis labels)
TiFlash 面向编译器的自动向量化加速