当前位置:网站首页>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 !
边栏推荐
猜你喜欢

Crack WinRAR to ad pop-up window

【OCR】AspriseOCR C# 英文、数字识别(中文不行)

C语言中主函数调用另外一个函数,汇编代码理解

Socket model of punctual atom stm32f429 core board

初学者入门阿里云haas510开板式DTU(2.0版本)--510-AS

Player actual combat 21 audio and video synchronization

注重点击,追求更多用户进入网站,可以选择什么出价策略?

What is automatic bidding? What are its advantages?

Axi4 increase burst / wrap burst/ fix burst and narrow transfer

基于Profibus-DP协议的PLC智能从站设计
随机推荐
Analysis of two-dimensional array passing as function parameter (C language)
Leetcode 2185. Counts the string containing the given prefix
Bridging and net
Detailed explanation of C language memset
To SystemC Beginners: the first program
Tcp/ip network communication knowledge record
Program analysis and Optimization - 6 loop optimization
Leetcode 2185. 统计包含给定前缀的字符串
Dismantle and modify the advertising machine - Amateur decompression
Player actual combat 21 audio and video synchronization
华为设备配置H虚拟专用网
QA of some high frequency problems in oauth2 learning
Player actual combat 14 display YUV
Dynamic search advertising intelligent search for matching keywords
【MySQL】数据库基本操作
Write policy of cache
Player actual combat 23 decoding thread
Reverse the encryption parameters of a hot water software
SystemC simulation scheduling mechanism
注重点击,追求更多用户进入网站,可以选择什么出价策略?