当前位置:网站首页>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 !
边栏推荐
- How to make a second clip of our media video without infringement
- The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
- PMP考试20天能通过吗?
- R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
- 关于memset赋值的探讨
- [learning notes] stage test 1
- 软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
- ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
- What are the advantages and characteristics of SAS interface
- 分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
猜你喜欢
申请代码签名证书时如何选择合适的证书品牌?
Tdengine biweekly selection of community issues | phase III
How can non-technical departments participate in Devops?
Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
Sharing the 12 most commonly used regular expressions can solve most of your problems
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)
How to protect user privacy without password authentication?
Principle and performance analysis of lepton lossless compression
家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
Thymeleaf th:with局部变量的使用
随机推荐
Total amount analysis accounting method and potential method - allocation analysis
[learning notes] stage test 1
How to protect user privacy without password authentication?
Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
Thymeleaf th:with use of local variables
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
让秒杀狂欢更从容:大促背后的数据库(下篇)
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
Judge whether the variable is an array
Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
C - Divisors of the Divisors of An Integer Gym - 102040C
Hongmeng fourth training
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
世界环境日 | 周大福用心服务推动减碳环保
mysql8.0JSON_CONTAINS的使用说明
动态规划
用“新”字来吸引好奇的人群
Disjoint Set
详解Vue适时清理keepalive缓存方案
R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates dynamic scatter animation (GIF) and uses shadow_ Mark function adds static scatter diagram as anim