当前位置:网站首页>Detailed explanation of structure
Detailed explanation of structure
2022-07-23 10:55:00 【VisionaryHS】
![]()
- 1. Declaration of a structure
struct tag { member-list; }variable-list;tag It can be understood as the label of structure ,variable-list Is the name of the structure , What is declared in braces is the data type , as follows
struct Stu { char name[20];// name int age;// Age char sex[5];// Gender char id[20];// Student number }; // You can't lose the semicolonThere are also special statements , Anonymous struct type :
// Anonymous struct type struct { int a; char b; float c; }x;2. Self reference of structure

It can be understood as a structure containing data fields and pointer fields , But the pointer field may be empty , ad locum ,struct Node*next It is a pointing type Node Structure pointer of , So it is the self reference of the structure .
3. Rename the structure

utilize typedef Name the structure weight , This can save a lot of redundant code .
4. Structure variable definition and initialization ( Examples )
struct Point { int x; int y; }p1; // Define variables while declaring types p1 struct Point p2; // Define structure variables p2 // initialization : Define variables and assign initial values at the same time . struct Point p3 = {x, y}; struct Stu // Type declaration { char name[15];// name int age; // Age }; struct Stu s = {"zhangsan", 20};// initialization struct Node { int data; struct Point p; struct Node* next; }n1 = {10, {4,5}, NULL}; // Structure nesting initialization struct Node n2 = {20, {5, 6}, NULL};// Structure nesting initialization5. Memory alignment of structures ( Calculate the size of the structure )
For its rules :1. The first member is offset from the structure variable by 0 The address of .
// practice 1
struct S1
{
char c1;
int i;
char c2;
};
printf("%d\n", sizeof(struct S1));// The result is 12
struct S3
{
double d;
char c;
int i;
};
printf("%d\n", sizeof(struct S3)); 
6. Change the default alignment number
#include <stdio.h>
#pragma pack(8)// Set the default alignment number to 8
struct S1
{
char c1;
int i;
char c2;
};
#pragma pack()// Unset the default number of alignments , Restore to default
#pragma pack(1)// Set the default alignment number to 1边栏推荐
- Analyse du code source et de la conception de redis - - 7. Liste rapide
- 一次 MySQL 误操作导致的事故,「高可用」都不好使了
- Redis source code and design analysis -- 11. Hash object
- Selenium JD crawler
- R language uses DALEX package to explain and analyze the machine learning model built by H2O package: summary and Practice
- N wars of C language -- common body and enumeration (VIII)
- 52832Dongle的安装
- Linux: database connection
- 【ROS进阶篇】第八讲 URDF文件的语法详解
- C語言基礎知識梳理(一)
猜你喜欢

C ivalueconverter interface usage example

C language n battle -- structure (VII)

TZC 1283: simple sort - heap sort

【Unity日常Bug】Unity报错Unexpected character ‘‘

部署storageclass踩坑记录

Basic knowledge of C language (I)

Exciting metauniverse! Wealth outlet of next generation Internet

Redis源码与设计剖析 -- 11.哈希对象

SVG, canvas, drawing line segments and filling polygon, rectangle, curve drawing and filling

Redis源码与设计剖析 -- 13.有序集合对象
随机推荐
全局事件总线
Redis源码与设计剖析 -- 5.整数集合
Redis源码与设计剖析 -- 12.集合对象
The wave of the meta universe is shocking. Seize the opportunity and work together
Kubernetes technology and Architecture (VI)
Redis source code and design analysis -- 9. String object
疫情时期加中年危机——游荡在十字街口的三个月
NOTIFIER诺帝菲尔消防主机电源维修及日常维护
Response object
【Unity日常Bug】Unity报错Unexpected character ‘‘
Meyer Burger梅耶博格西门子工控机维修及机床养护
华为高层谈 35 岁危机,程序员如何破年龄之忧?
Global event bus
3dMax先蒙皮刷权重,再附加合并
Thing JS notes
0 basic career change software test, the necessary skills with a monthly salary of 6000 and 11000 are quite different
Deploy metersphere
mysql的索引的操作
Important knowledge of application layer (interview, reexamination, term end)
Niuke brush questions - Sword finger offer (phase II)