当前位置:网站首页>C language structure
C language structure
2022-07-20 19:40:00 【Fang XS.】
C Language structure
- Fang XS.
- [email protected]
- If there is an error , Hope to be pointed out , The road of learning technology will inevitably be bumpy , Irregular update
Structure
- stay C in , A structure is a user-defined data type , Allow users to store different data types .
- stay C in , Basic data types can be stored in the structure , Composite data type , The pointer .
- stay C in , A struct is a data set consisting of a series of data of the same or different types .
- stay C in , All the same / Different types of data can be encapsulated with structures .
- stay C++ in , Structures can contain functions ,C You can't , But you can use function pointers .
- grammar
- The first way :
struct struct_name{ int struct_member; };
. typedef
The way :typedef struct _struct_name{ int struct_member; }struct_name;
- The first way :
- Defining variables
- The first way :
struct struct_name{ int struct_member; }struct_var;
Define structure variables directly before semicolons .struct struct_name struct_var;
typedef
The way :struct_name struct_var;
- The first way :
- Use
/* Use typedef Defining structure */
typedef struct _struct_name{
int struct_member;
}struct_name;
int main(void)
{
/* Use structure pointers */
struct_name * pStructure = (struct_name *)malloc(sizeof(struct_name));
pStructure->struct_member = 3;
printf("struct_member: %d\n", pStructure->struct_member);
free( pStructure ); // Free memory
pStructure = NULL; // Null pointer
struct_name Structure = {
5 }; // Direct initialization
printf("struct_member: %d\n", Structure.struct_member);
}
- Anonymous structure :
/* Usage mode :*/
struct{
int member;
}anonymous_struct;/* Directly defines anonymous_struct Global variables */
typedef struct _struct_name{
int struct_member;
struct {
int struct_nesting_member; /* Nested anonymous structure */
};
}struct_name;
int main(void)
{
anonymous_struct.member = 8 ; // Use it directly
printf("struct_member: %d\n", anonymous_struct.member);
struct_name Structure = {
5 ,3 };
printf("struct_member: %d\n", Structure.struct_member);
printf("struct_nesting_member: %d\n", Structure.struct_nesting_member);
return 0;
}
- Structure is used as function parameter
typedef struct _struct_name{
int struct_member1;
int struct_member2;
}struct_name;
void print_struct_1(struct_name s){
// Value passed
printf("struct_member1: %d\n", s.struct_member1);
printf("struct_member2: %d\n", s.struct_member2);
}
void print_struct_2(struct_name *s){
// Pointer passing
printf("struct_member1: %d\n", s->struct_member1);
printf("struct_member2: %d\n", s->struct_member2);
}
/** * The difference between value passing and pointer passing is : * When the structure is large , Value transfer is copying data , Inefficient and takes up memory . * At this time, using structure pointer is much faster , The data of access structure is accessed indirectly through pointers . * No matter how big the structure is , It's just a 4 Byte pointer . */
int main(void)
{
struct_name Structure = {
5 ,3 };
print_struct_1( Structure );
print_struct_2( &Structure );
return 0;
}
- The size of structure in memory
struct sTest1{
char member1; // 1
int member2; // 4
short member3; // 2
};
struct sTest2{
char member1; // 1
short member2; // 2
int member3; // 4
};
struct sTest3{
char member1; // 1
char member2[2]; // 2
char member3[4]; // 4
};
/** * The size of the structure in bytes , Its memory layout needs to be considered , Structures are stored in memory by unit * The size of each unit depends on the size of the largest basic type in the structure . */
int main(void)
{
// Basic data type size
printf("sizeof char = %d\n", sizeof(char)); // 1 byte
printf("sizeof short = %d\n", sizeof(short)); // 2 byte
printf("sizeof int = %d\n", sizeof(int)); // 4 byte
printf("sizeof struct1 = %d\n", sizeof(struct sTest1)); // 12
printf("sizeof struct2 = %d\n", sizeof(struct sTest2)); // 8
printf("sizeof struct3 = %d\n", sizeof(struct sTest3)); // 7
return 0;
}
- Bit field
struct sTest1{
char member1 : 2; //member1 yes char The type accounts for 2 position .
char member2 : 1; //member2 yes char The type accounts for 1 position .
char member3 : 3;
char : 1; // The nameless bit field is used to occupy bits , Indicates that data is not stored , Considering memory alignment requires .
char member5 : 1;
};
/** * Structure member plus : Take the numbers , Represents the number of bits occupied by this member variable . * such as member2 Only in memory 1 position , yes char Type data range [0,1]. Can only be 0 perhaps 1. * C Language standards , The width of a bit field cannot exceed the length of the data type it is attached to . * That is, the type of member variable limits the maximum range , Using bit fields can limit the specific range of member variables , Reasonable use saves memory . */
int main(void)
{
printf("sizeof struct1 = %d\n", sizeof(struct sTest1)); // 1 byte
return 0;
}
- The structure is in C Widely used in , A wide range of uses .
- The member variables inside the structure are function pointers , By modifying the function pointed to by the function pointer , Can achieve C Language runtime ‘ polymorphic ’.
- Simple classes can be implemented using structs and function pointers , It can be used C To object-oriented programming , such as Linux.
边栏推荐
- [open source] magicdata Ramc: 180 hour Chinese conversational voice data set officially released
- TST, CMP, bne, BEQ instructions
- DeFi 2.0的LaaS协议Elephant,或许是你熊市下为数不多的获利手段
- 使用 Abp.Zero 搭建第三方登录模块(三):网页端开发
- window常用API
- koa2学习
- [cloud co creation] design Huawei cloud storage architecture with the youngest cloud service hcie (Part 2)
- Redis cluster setup (one master, two slave and three sentinels) complete version with verification report
- Jz2440 development board TFT LCD experiment
- Fedora TFTP configuration and test method
猜你喜欢
随机推荐
关于GCC编译常用命令
golang exec.Command使用
Keil uVision5代码自动补全或代码联想
Magichub重口音对话语音识别挑战赛报名开始
【数组及数组的常用操作】
干货 | 训练AI模型找不到数据?20个精选开源社区收藏好!
Mysql常用语句知识点
使用 Abp.Zero 搭建第三方登录模块(四):微信小程序开发
Wechat applet development uses onreachbottom to realize page bottom loading and paging
Deep learning - deep understanding of normalization and batchnorm (theoretical part)
PDA-RBCs-NPs 聚多巴胺修饰的红细胞-纳米粒子复合/透明质酸包裹马钱子碱牛血清蛋白纳米粒的制备
【开源】MagicData-RAMC :180小时中文对话式语音数据集正式发布
2022年度“强国杯”初赛wp(有脚本,过程详细)
Preparation of inh-rfp-bsa-nps loaded INH and RFP albumin nanoparticles / capataxel loaded albumin nanoparticles
S3c2440 u-boot migration - norflash driver support - s29al016u-boot version: 2008.10 development board: mini2440
PIC16F877XA指令系统(汇编语言)
MLX90640 红外热成像仪测温模块开发笔记(一)
开发者必读:2022年移动应用运营增长洞察白皮书
Redis cluster setup (one master, two slave and three sentinels) complete version with verification report
一阶互补滤波应用到stm32 ADC采集上