当前位置:网站首页>Self taught structure (small turtle C language)
Self taught structure (small turtle C language)
2022-06-29 17:20:00 【Fecter11】
The following is in b The little turtle watching from the station c Language when recorded with video code
What kind of structure is it , How to use a structure ?
Take the following code as an example
# include <stdio.h>
struct Book // Define the key of a structure struct And the name of the structure is Book Note that the initial is usually capitalized
{
char title[128]; // Our function is to store the name of the book , author , And the price , Plate number
char author[48]; // Inside the braces are structural members
float price;
unsigned int date;
char pulisher[48];
}book; // This is the name of the structure variable , Be similar to int book Medium book Is the variable name of the integer , there book Is the structure type , The structure is called Book The variable of book. There is a little detour here .
void main(void)// The main function
{
While(1);// Do nothing for the time being
}
Another way to define a structure is
# include <stdio.h>
struct Book // Define the key of a structure struct And the name of the structure is Book Note that the initial is usually capitalized
{
char title[128]; // Our function is to store the name of the book , author , And the price , Plate number
char author[48];
float price;
unsigned int date;
char pulisher[48];
}; // Note that only the name of the structure is defined here , Instead of creating variables .
void main(void)// The main function
{
Struct Book book;// Here is the creation of a Book Variable of structure type book
}
The next step is how to access structure variables ?
The member of the access structure introduces a new symbol “ Order number ”——“.”
such as book.title It's quoting Book Structure of the title member , obviously title The member is an array of characters .
and book.price Is quoted Book Structure of the price member , and price Is a floating-point variable .
Next, continue to look at the program
# include <stdio.h>
struct Book
{
char title[128];
char author[48];
float price;
unsigned int date;
char pulisher[48];
}book;
void main(void)
{
printf(" Please enter the title of the book \n");
scanf("%s",book.title); // This involves assignment
printf(" Please enter the author \n");
scanf("%s",book.author);
printf(" Please enter the selling price \n");
scanf("%f",&book.price);
printf(" Please enter the date of publication \n");
scanf("%d",&book.date);
printf(" Please enter the publishing house \n");
scanf("%s",book.pulisher);
printf("************ Data entry completed ************\n");
printf(" Title :%s\n",book.title);
printf(" author :%s\n",book.author);
printf(" The price is :%f\n",book.price);
printf(" Publication date :%d\n",book.date);
printf(" Press. :%s\n",book.pulisher);
}
Initializing structure variables
Initialize a variable and an array
Int a = 520;
Int array[5]={
1,2,3,4,5};
Initialize a structure variable
Struct Book book ={
“ Little turtle takes you flying ”, // Be careful to separate them with commas , Also note that it must correspond to the defined structure member types
“ Little turtle ”,
9.9,
20170707,
“ tsinghua ”
};
Initializes the specified member value of the structure
Its syntax is similar to that of an array specifying initialization elements , However, the struct specifies that initialization members use “ Order number ”——“.” Operator and member name
For example, initialization book Of price member :
Struct Book book = {
.price = 9.9};
You can also initialize without the order of members declared by the structure :
Struct Book book = {
.publisher = “ tsinghua ”,
.price = 9.9,
.date = 20170707
};
The system will align the memory
# include <stdio.h>
void main (void)
{
struct A
{
char a;
int b;
char c;
}a = {
'x',250,'c'};
printf("size of a = %d \n",sizeof(a));
}
Theoretically, the above procedure is x Station one byte ,250 Occupy 4 Bytes ,c Take up a byte
The actual output is 12 Bytes
Each of them is in accordance with 4 Byte processing is old 12 Bytes
If you change the order of output x,o,250
The output of 8 Bytes
because , The first two character types each occupy 2 Bytes , But the system will automatically supplement 2 Bytes , Take up a total of 4 Bytes , and 250 It is assigned by itself 4 Bytes .
Structure array and structure pointer
Nested structure
# include <stdio.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title[128];
char author[48];
float price;
struct Date date;// junction á structure 1 body ? Of ? Embedded ? set ?
char pulisher[48];
}book = {
" Little turtle ,
" Little turtle ",
9.9,
{
2017,1,11},
" Tsinghua University "
};
void main(void)
{
printf(" Title :\n",book.title);
printf(" author :%s\n",book.author);
printf(" The price is :%f\n",book.price);
printf(" Publication date :%d-%d-%d\n",book.date.year,book.date.month,book.date.day); // Be sure to access the bottom layer
printf(" Press. :%s\n",book.pulisher);
}
Array of structs
The first one is : Define when declaring a structure
Struct Structure name
{
Structural members ;
} Array name [ length ];
The second kind : A structural type is indicated first ( Like the one above Book), Then define a structure array with this type .
Struct Structure name
{
Structural members ;
};
Struct Structure name Array name [ length ];
Initializing the array of structs
Struct Book book[3] = {
{“< Zero basis >”,” Little turtle ”,9.9,{2017,11,7},” tsinghua ”},
{“ introduction ”,” Soft shelled turtle ”,9.9,{2017,10,1},” tsinghua ”},
{“ To the earth ”,“ fish ”,9.9,{2017,9,9},” tsinghua ”},
};
Structure pointer
Struct Book * pt; // Make a statement Book Pointer variable of type structure pt
Here's a little bit of attention , The variable name of the array represents the first address of the array , So the array name can be directly assigned to the pointer variable . The variable name of the structure pointer needs to use “&” The address can be given to the structure variable .
We need to add one more pt = &book;
There are two ways to access structure members through structure pointers :
The first one is :
( Structure pointer ). Member name
Be careful : The parentheses are added here because “.” Operators take precedence over “” Operator .
The second kind :
Structure pointer -> Member name
Be careful “->” There must be no space between them , In addition, his priorities are similar to “.” The priority of is the same .
The above two are completely equivalent
The first way to quote is as follows :
# include <stdio.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title[128];
char author[48];
float price;
struct Date date;// junction á structure 1 body ? Of ? Embedded ? set ?
char pulisher[48];
}book = {
" Little turtle flies ,
" Little turtle ",
9.9,
{
2017,1,11},
" Tsinghua University "
};
void main(void)
{
struct Book*pt;
pt = &book;
printf(" Title :%s\n",(*pt).title);
printf(" author :%s\n",(*pt).author);
printf(" The price is :%0.2f\n",(*pt).price);
printf(" Publication date :%d-%d-%d\n",(*pt).date.year,(*pt).date.month,(*pt).date.day);
printf(" Press. :%s\n",(*pt).pulisher);
}
The second way to quote
# include <stdio.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title[128];
char author[48];
float price;
struct Date date;// Nesting of structures
char pulisher[48];
}book = {
" Little turtle flies ,
" Little turtle ",
9.9,
{
2017,1,11},
" Tsinghua University "
};
void main(void)
{
struct Book*pt;
pt = &book;
printf(" Title :%s\n",pt->title);
printf(" author :%s\n",pt->author);
printf(" The price is :%0.2f\n",pt->price);
printf(" Publication date :%d-%d-%d\n",pt->date.year,pt->date.month,pt->date.day); printf(" Press. :%s\n",pt->pulisher);
}
边栏推荐
- 关于harbor私有仓库忘记登录密码
- R语言epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、aggregate.data.frame函数包含缺失值的情况下分组统计结果为NA
- Freedom自由协议质押挖矿系统开发
- 关于KALI使用xshell连接
- 适合中小企业的项目管理系统有哪些?
- 微博评论高性能高可用架构设计(架构实战营 模块五作业)
- PancakeSwap技术:夹子机器人系统开发原理
- Why is informatization ≠ digitalization? Finally someone made it clear
- 6.25atcoderabc257e - addition and multiplication 2
- 最高81.98%!超百所“双一流”高校本科深造率公布
猜你喜欢
随机推荐
flink sql rownumber 报错。谁遇到过啊?怎么解决?
6.26cf simulation game d: black and white questions
[the sixth operation of modern signal processing]
正则表达式
Kubernetes deployment dashboard (Web UI management interface)
外部自动(PLC启动机器人)
mysql查询视图命令是哪个
Leetcode daily question - 535 Encryption and decryption of tinyurl
A user level thread library based on C language
mysql视图能不能创建索引
疫情居家外包项目之协作开发| 社区征文
mysql如何查询表的字符集编码
SLAM中的子图
Subgraphs in slam
可转债策略之---(摊饼玩法,溢价玩法,强赎玩法,下修玩法,双低玩法)
垃圾收集器
mysql.sock的概念是什么
有遇到用flink-cdc采集MySQL-RDS的时候,datetime类型的字段,采集过来后和源表
First batch! Tencent cloud's ability to pass the solution of the government affairs collaboration platform of the China Academy of ICT
在供应链场景应用中,供应链管理系统扮演什么角色?








