当前位置:网站首页>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 !
边栏推荐
- If you want to build brand awareness, what bidding strategy can you choose?
- Three common methods of C language array initialization ({0}, memset, for loop assignment) and their principles
- Two methods of QT using threads
- Detailed explanation of C language memset
- Design of PLC intelligent slave station based on PROFIBUS DP protocol
- QT to realize the simple use of SQLite database
- 通信流量分析
- How to package QT program learning records with inno setup
- Reverse order of Excel
- Player practice 20 audio thread and video thread
猜你喜欢

Player practice 26 adding slider and window maximization

如何使用android studio制作一个阿里云物联网APP

TestEngine with ID ‘junit-vintage‘ failed to discover tests

Implementation and debug of process hiding under x64

Perfect ending | detailed explanation of the implementation principle of go Distributed Link Tracking

Reverse order of Excel

程序分析与优化 - 6 循环优化

Player practice 18 xresample

Leetcode 2176. Count equal and divisible pairs in an array

新技术:高效的自监督视觉预训练,局部遮挡再也不用担心!
随机推荐
Detailed explanation of C language memset
Alicloud development board vscode development environment setup
Reverse order of Excel
Sorting out the differences between ABS () and Fabs () in C language
高考回忆录
The original Xiaoyuan personal blog project that has been around for a month is open source (the blog has basic functions, including background management)
1414. minimum number of Fibonacci numbers with sum K
Postgresql14 installation and use tutorial
Notepad common settings
OAuth2学习中的一些高频问题的QA
Brush one question every day /537 Complex multiplication
Player actual combat 14 display YUV
Player practice 20 audio thread and video thread
After reading the question, you will point to offer 16 Integer power of numeric value
Nesting of C language annotations
Word insert picture blocked by text
QT database realizes page turning function
Sizeof calculation space size summary
My resume.
Tlm/systemc: TLM socket binding problem