当前位置:网站首页>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 !
边栏推荐
- QT database realizes page turning function
- 正点原子STM32F429核心板的插座型号
- Player actual combat 22 to solve the problems of flower screen and Caton
- Player practice 20 audio thread and video thread
- SystemC time
- QT multi thread drawing and real-time refreshing method
- Player actual combat 23 decoding thread
- CSDN blog points rule
- Communication flow analysis
- Leetcode 2185. Counts the string containing the given prefix
猜你喜欢

Des File Encryptor based on MFC framework

肝了一个月的原创小袁个人博客项目开源啦(博客基本功能都有,还包含后台管理)

In C language, the main function calls another function, which is understood by assembly code

通信流量分析

正点原子STM32F429核心板的插座型号

QT realize picture dragging

Two methods of implementing asynchronous calling function with QT

Pay attention to click and pursue more users to enter the website. What bidding strategy can you choose?

Crack WinRAR to ad pop-up window

SystemC common errors
随机推荐
Why do Chinese programmers change jobs?
Lua tvalue structure
How to use Android studio to create an Alibaba cloud Internet of things app
Axi4 increase burst / wrap burst/ fix burst and narrow transfer
TestEngine with ID ‘junit-vintage‘ failed to discover tests
Lua callinfo structure, stkid structure resolution
华为设备配置BGP AS号替换
Player practice 26 adding slider and window maximization
Tcp/ip network communication knowledge record
工业机械臂(机器人)视觉定位引导系统
TestEngine with ID ‘junit-vintage‘ failed to discover tests
How test engineers transform test and open
Player actual combat 21 audio and video synchronization
C secret script Chapter 1: data storage (in-depth analysis)
Ppt cannot be opened, always prompt how to fix it
Leetcode 2176. Count equal and divisible pairs in an array
English learning plan
Running phase of SystemC
My resume.
Getting started alicloud haas510 open board DTU (version 2.0) --510-as