当前位置:网站首页>C language: 15. Structure
C language: 15. Structure
2022-07-27 19:33:00 【Brother rabbit head】
c Language :15、 Structure
1、 Structure definition
The way structure is defined :
// The way 1
struct weapon{
char *name;
int price;
};
struct weapon weapon_1 = {
18};
weapon_1.name = "asdf";
// The way 2
struct weapon2{
char name[20];
int price;
}weapon2_1;
printf("%d \n", weapon2_1.price);
struct weapon2 wp = {
"abcccc",123};
// The way 3
struct{
char name[20];
int price;
}struct_var;
printf("%d \n",struct_var.price);
coordination typedef Defining structure :
Insert a code chip here
2、 Structure initialization and use
struct weapon{
char name[20];
int atk;
int price;
}
struct weapon weapon_1 = {
" weapons 1",100,200};
weapon_1.price++;
printf("%s The price of is :%d\n",weapon_1.name, weapon_1.price);
3、 Array of structs
struct weapon{
char name[20];
int price;
}
struct weapon weapon_1[2] = {
" weapons 1",100,
" weapons 2",200
};
struct weapon weapon_2[2] = {
{
" weapons 1",100},
{
" weapons 2",200}
};
printf("%s The price of is :%d\n", weapon_1[0].name, weapon_1[1].price);
4、 Structure pointer
The pointer points to the structure variable
struct weapon{
char name[20];
int price;
};
struct * w;
struct weapon weapon1 = {
" weapons 1", 100};
w = &weapon1;
printf(" weapons %s The price of is %d\n", (*w).name, (*w).price );
// The upper and lower methods of accessing structure pointer are equivalent
printf(" weapons %s The price of is %d\n", w->name, w->price );
The pointer points to the structure array
#include <stdio.h>
struct weapon {
char name[20];
int price;
};
int main(){
struct weapon wea_arr1[2] = {
" net ",200,
" Dragon Slayer ",201
};
struct weapon *p;
p = wea_arr1;
printf("[%s] The price of is [%d]\n", (*p).name, (*p).price );
p++;
printf("[%s] The price of is [%d]\n", p->name, p->price );
return 0;
}
5、 Structure memory alignment
typedef struct {
char a;//1
char b;//1
int c;//4
short d;//2
double e;//8
} Align;
Align align = {
'a','b',3,4,5.0};
typedef struct {
char a;//1
char b;//1
short d;//2
int c;//4
double e;//8
} Align2;
Align2 align2 = {
'a','b',3,4,5.0};


The above example shows , Defining the same types together in the structure can save memory .
Except that the same types are defined together , Different editors , Different c Language versions define different ways of memory alignment optimization , Such as :
#pragma pack(2) // Almost all editors support this writing , Take effect globally after use
struct Person{
char *name
__attribute((aligned(2))) int c;// Only gcc Be able to use ( Align only one field )
_Alignas(4) int c;// Only c11 have access to ( Align only one field )
}
边栏推荐
- c语言:15、结构体
- golang设置国内镜像,vscode配置golang开发环境,vscode调试golang代码
- Responsibility should be assigned to people, and Guangzhou should take multiple measures to build a "safety line" for children in summer
- kettle switch / case 控件实现分类处理
- c语言:7、c语言多源码文件使用方法
- An experience
- Idea optimization strategy
- Rs2022/ cloud detection: semi supervised cloud detection in satellite images by considering the
- The great idea of NS2
- Basic network faults and troubleshooting
猜你喜欢
随机推荐
零知识证明的硬件加速
Kettle separate and merge records
Kettle learning - the repository configuration in version 8.2 is grayed out, and there is no connect button
一篇让你掌握线程和线程池,还解决了线程安全问题,确定不看看?
MongoDB学习笔记(1)——安装MongoDB及其相关配置
To create a MySQL data source resource group, you must choose to create a new exclusive data integration resource group? Or use a common resource group? thank you
利用 Fastjson json (简单粗暴版)
Rename file with command line
Anaconda下安装Talib库
sql 字段类型转换
idea优化小攻略
v-if,v-else,v-for
来一遍《剑指Offer》03. 数组中重复的数字
Role authorization --- complete the addition and deletion of secondary menus by adding and deleting primary menus
200行代码快速入门文档型数据库MonogoDB
MFC高级控件之Tab控件( CTabCtrl )
opds sql 里面可以用set 定义局部变量吗
Word 2007+ tips
Webmagic+selenium+chromedriver+jdbc grabs data vertically.
Opening and using Alibaba cloud object storage OSS









