当前位置:网站首页>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);
}
边栏推荐
- Collaborative development of epidemic home outsourcing project 𞓜 community essay solicitation
- Help MySQL data analysis with databend
- Kubernetes部署Dashboard(WEB UI管理界面)
- 关于Go中两个模块互相调用的场景解决方案
- Naacl 2022 | distillation of machinetranslation SOTA model
- 力扣解法汇总535-TinyURL 的加密与解密
- 深圳内推 | 深圳计算科学研究院招聘机器学习助理工程师(校招)
- 线段树、树状数组模板(复制粘贴确实好用)
- 使用 SSH 方式拉取代码
- Summary of problems during xampp Apache installation
猜你喜欢
随机推荐
Browser large screen capture
sectigo ov泛域名证书一年一千五百九十元好用吗
在供应链场景应用中,供应链管理系统扮演什么角色?
R语言epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、aggregate.data.frame函数包含缺失值的情况下分组统计结果为NA
c# 国内外ORM 框架 dapper efcore sqlsugar freesql hisql sqlserver数据常规插入测试性能对比
mysql在linux中2003错误如何解决
“授权同意”落地压力大?隐私计算提供一种可能的合规“技术解”
机器学习8-人工神经网络
最高81.98%!超百所“双一流”高校本科深造率公布
疫情居家外包项目之协作开发| 社区征文
Leetcode 984. 不含 AAA 或 BBB 的字符串(网友思路)
腾讯云发布自动化交付和运维产品Orbit,推动企业应用全面云原生化
About Kali using xshell connection
微博评论高性能高可用架构设计(架构实战营 模块五作业)
Understanding adapter mode from home office | community essay solicitation
regular expression
KUKA robot external axis configuration what you must know
Automatic vending machine
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将一个ggplot2可视化结果和一个plot函数可视化结果横向组合起来形成最终结果图
MySQL触发器如何创建与删除









