当前位置:网站首页>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 !
边栏推荐
- World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
- The forked VM terminated without saying properly goodbye
- Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
- 如何将 DevSecOps 引入企业?
- Current situation, trend and view of neural network Internet of things in the future
- 不相交集
- The function of qualifier in C language
- Postman简介、安装、入门使用方法详细攻略!
- SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
- 矩阵链乘 - 动态规划实例
猜你喜欢
TiCDC 6.0原理之Sorter演进
世界环境日 | 周大福用心服务推动减碳环保
家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
Redis如何实现多可用区?
Section - left closed right open
TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析
How to protect user privacy without password authentication?
SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
随机推荐
How to introduce devsecops into enterprises?
3W原则[通俗易懂]
ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
无密码身份验证如何保障用户隐私安全?
What is the future development trend of neural network Internet of things
详解Vue适时清理keepalive缓存方案
Principle and performance analysis of lepton lossless compression
区间 - 左闭右开
Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products
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
Judge whether the variable is an array
美国费城发生“安全事故” 2名警察遭枪杀
R language ggplot2 visual density map: Visual density map by group and custom configuration geom_ The alpha parameter in the density function sets the image transparency (to prevent multiple density c
Sharing the 12 most commonly used regular expressions can solve most of your problems
Linux下mysql数据库安装教程
关于memset赋值的探讨
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景