当前位置:网站首页>C secret arts script Chapter 5 (structure) (Section 1)
C secret arts script Chapter 5 (structure) (Section 1)
2022-06-12 14:24:00 【Mortal programming biography】
I haven't seen you for many days , Dear Taoist friends , Here comes our new chapter , The data types we saw in the previous chapters are c Language built-in data types , Such as int,float,char.... We know ,c Language has custom functions , Users can design a function according to the functions they need , Does our data type have a user-defined type ? The answer is yes : The first one we want to introduce in this chapter Custom data types —— Structure .
1. What is a structure : A structure is a collection of values , These values are called member variables , Each member of the structure can be a variable of different data types .
2, Declaration and definition of structure
#include<stdio.h> struct stu { char name[6]; int age; double score; // Declaration of a structure }; int main() { struct stu s1={ {" Zhang San "},{17},{456.0}}; // Definition of structure return 0; }Be careful :
1. It's defined in main Structural variables within functions or other functions are local variables , If created in struct{} Variable name ; Here is the global variable .
The following code :
#include<stdio.h> struct stu { char name[6]; int age; double score; // Declaration of a structure }stu; // Global variable definition of structure2. Anonymous declaration of structure :
Such as :
#include<stdio.h> struct { int a; char b; float c; };This structure has no definition. The structure data type name cannot be in main Function or other functions , Only in struct{} Variable name ; Here to create .
Such as :
#include<stdio.h> struct { int a; char b; float c; }S;3. Self reference of structure
It sounds like functional recursion , Call yourself .
Such as :
#include<stdio.h> struct node { int cor; struct node n; }; int main() { sizeof(struct node); return 0; }It looks like there's nothing wrong with it , But this code is wrong ,struct node n Call yourself , Each structure has a int cor, Then call yourself ,sizeof How big is the calculated space , This is immeasurable, so this structure self reference is wrong .
Correct self reference method :
#include<stdio.h> struct node { int cor; struct node* n; }; int main() { sizeof(struct node); return 0; }We write self reference members as structure pointers , Only pointers can find structures of the same type , And the size of the pointer is fixed 4/8.
4.typedef Keyword decorated structure
typedef Keyword can rename the data type , Structure types are no exception .
Such as :
#include<stdio.h> typedef struct node { int a; char b; }node;Be careful :typedef A modified structure {}; The variables between are not global variables , It was renamed node Of struct node Structure type name .
2.typedef Decorate anonymous structures
#include<stdio.h> typedef struct { int a; char b; }node;This writing is wrong , The structure type has not been named yet , How to rename ? Isn't it a question of chicken or egg first ?
Okay , That's all for this section , Dear Taoist friends , The coming days would be long , I'll see you in the next section !
边栏推荐
- 【活动早知道】LiveVideoStack近期活动一览
- [ROC] aspriseocr C # English, Digital identification (not Chinese)
- The original Xiaoyuan personal blog project that has been around for a month is open source (the blog has basic functions, including background management)
- Write policy of cache
- Running phase of SystemC
- 动态搜索广告智能查找匹配关键字
- How to use Android studio to create an Alibaba cloud Internet of things app
- Analysis of two-dimensional array passing as function parameter (C language)
- 程序分析与优化 - 6 循环优化
- OAuth2学习中的一些高频问题的QA
猜你喜欢

Design of PLC intelligent slave station based on PROFIBUS DP protocol

What is automatic bidding? What are its advantages?

Dynamic search advertising intelligent search for matching keywords

Socket model of punctual atom stm32f429 core board

If you want to build brand awareness, what bidding strategy can you choose?

拆改廣告機---業餘解壓

Recursive summary of learning function

【活动早知道】LiveVideoStack近期活动一览

Player actual combat 25 unpacking module add close

Tcp/ip network communication knowledge record
随机推荐
使用make方法创建slice切片的坑
Dial up and Ethernet
2022版Redis数据删除策略
Wait function in SystemC
Player practice 18 xresample
华为设备配置BGP AS号替换
Why do Chinese programmers change jobs?
[early knowledge of activities] list of recent activities of livevideostack
Player practice 19 xaudio turn on audio
动态搜索广告智能查找匹配关键字
SystemC uses SC_ report_ Handler processing log printing
拆改广告机---业余解压
Detailed explanation of C language memset
C secret script Chapter 1: data storage (in-depth analysis)
Analysis of lua source code
Lua callinfo structure, stkid structure resolution
Solve the problem that IEEE latex template cannot display Chinese
如何使用android studio制作一个阿里云物联网APP
Leetcode 2176. 统计数组中相等且可以被整除的数对
对某热水软件的加密参数逆向