当前位置:网站首页>How to initialize "naming and surname" in C language
How to initialize "naming and surname" in C language
2022-04-23 01:46:00 【Traceless rain】
Specify the concept of initializer
C90 The standard requires that the elements in the initialization program appear in a fixed order , In the same order as the elements in the array or structure to be initialized . But in the new standard C99 in , Added a new feature : Specify initializer . Using this feature, you can initialize the specified array or structure element .
The specified initializer of the array
The specified initializer of a one-dimensional array
Use the properties of the specified initializer , We can define and initialize an array like this :
-
int a[6] = {[4] = 10,[2] = 25};
The above initialization is equivalent to the following way :
-
int a[6] = {0,0,25,0,10,0};
You can see that in this way, you can not follow the order , And specify specific elements for initialization . In addition to the above usage , We can also initialize a range of elements in the array ( Notice this is GCC Extended syntax for , instead of C99 Standards for ), Such as this :
-
int a[5] = {[4] = 10,[0 ... 3] = 23};
The initialization of the above program is equivalent to the following initialization :
-
int a[5] = {23,23,23,23,10};
If there are specified elements in array initialization and unspecified elements in array initialization, how to analyze them ? Such as this :
-
int a[5] = {11,[3] = 44,55,[1] = 22,33};
Then it is equivalent to the following code :
-
int a[5] = {11,22,33,44,55};
If the size of the array is not specified when defining the array , What is the actual size of the array ? Such as this :
-
int main(void)
-
{
-
int number[] = {[20] = 1,[10] = 8,9};
-
int n = sizeof(number)/sizeof(number[0]);
-
printf("The Value of n is:%d\n",n);
-
}
The output is like this :
-
The Value of n is:21
in other words , If the size of the array is not given , The maximum initialization position determines the size of the array
The specified initializer of a two-dimensional array
Two dimensional arrays can also use the method of specifying initializers , The following is the initialization of a two-dimensional array :
-
int array[2][2] =
-
{
-
[0] = {[0] = 11},
-
[1] = {[1] = 22},
-
};
Such initialization is equivalent to the following code :
-
int array1[2][2] =
-
{
-
{11,00},
-
{00,22}
-
};
By the above code , We can also know , In the method of specifying initializer of two-dimensional array , first []
The number in represents the number of rows of the initialized two-dimensional array , And in the {}
The inside initializes the elements of the current line , Actually, that is to say {}
The initialization method in is the same as that of one-dimensional array , A feasible way of one-dimensional array , Two dimensional arrays are also feasible .
application
After describing the basic concept of array specifying initializer , Let's take a concrete example , The following example is based on the programming method of state machine ATM machine , First ATM It has the following states ;
We can use the idea of state machine to write this program , First, use enumeration to define each state and corresponding operation :
-
typedef enum
-
{
-
Idle_State,
-
Card_Inserted_State,
-
Pin_Entered_State,
-
Option_Selected_State,
-
Amount_Entered_State,
-
last_State
-
}eSysyemState;
-
typedef enum
-
{
-
Card_Insert_Event,
-
Pin_Enter_Event,
-
Option_Selection_Event,
-
Amount_Enter_Event,
-
Amount_Dispatch_Event,
-
last_Event
-
}eSystemEvent;
Then there is the specific implementation of the corresponding operation :
-
eSysyemState AmountDispatchHandler(void)
-
{
-
return Idle_State;
-
}
-
eSysyemState EnterAmountHandler(void)
-
{
-
return Amount_Entered_State;
-
}
-
eSysyemState OptionSelectionHandler(void)
-
{
-
return Option_Selected_State;
-
}
-
eSysyemState InsertCardHandle(void)
-
{
-
return Card_Inserted_State;
-
}
-
eSysyemState EnterPinHandler(void)
-
{
-
return Pin_Entered_State;
-
}
To make the implementation of the state machine look less verbose , We use the way of looking up tables here , First redefine a function pointer two-dimensional array type :
-
typedef eSysyemState (* const afEventHandler[last_State][last_Event])(void);
Simply put, this is a two-dimensional array , The function pointer is stored in the two-dimensional array , This function pointer points to the return value of eSysyemState, The formal parameter is void Function of . After redefining the type , We can use it to define new variables , before this , Add a little bit of array related content , For example, there is the following code :
-
typedef int array[3];
-
array data;
Then the above code is equivalent to the following code :
-
int data[3];
With the above code , We can implement our lookup table , The specific code is as follows :
-
static afEventHandler StateMachine =
-
{
-
[Idle_State] = {[Card_Insert_Event] = InsertCardHandle},
-
[Card_Inserted_State] = {[Pin_Enter_Event] = EnterPinHandler },
-
[Pin_Entered_State] = {[Option_Selection_Event] = OptionSelectionHandler},
-
[Option_Selected_State] = {[Amount_Entered_Event] = EnterAmountHandler},
-
[Amount_Entered_State] = {[Amount_Dispatch_Event] = AmountDispatchHandler},
-
};
Now let's look at the initialization method, which is more clear , This is actually a method of parsing a two-dimensional array using the specified initializer , Last , That is, our state machine running code :
-
#include <stdio.h>
-
int main(void)
-
{
-
eSysyemState eNextState = Idle_State;
-
eSystemEvent eNewEvent;
-
while(1)
-
{
-
eNewEvent = ReadEvent();
-
/* Omit relevant judgment */
-
eNextState = (*StateMachine[eNextState][eNewEvent])();
-
}
-
return 0;
-
}
The specified initializer of the structure
The following structures are defined :
-
struct point
-
{
-
int x,y;
-
}
Then the initialization of structure variables can be done in the following ways :
-
struct point p =
-
{
-
.y = 2,
-
.x = 3
-
};
The above code is equivalent to the following code :
-
struct point p = {3,2};
What's the use of such initialization ? Here is linux A piece of kernel code :
-
const struct file_operations eeprom_fops =
-
{
-
.llseek = eeprom_lseek,
-
.read = eeprom_read,
-
.write = eeprom_write,
-
.open = eeprom_open,
-
.release = eeprom_close
-
};
The above is to initialize by specifying the initializer , among file_operations There are many members in this structure , The above initialized members are only part of them ,
-
struct file_operations {
-
struct module *owner;
-
loff_t (*llseek) (struct file *, loff_t, int);
-
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
-
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
-
int (*open) (struct inode *, struct file *);
-
int (*flush) (struct file *, fl_owner_t id);
-
int (*release) (struct inode *, struct file *);
-
/* There are still a lot of it , Omit */
-
}
Using this method of specifying initializers , Flexible use , And the code is easy to maintain . Because if you assign values in a fixed order , When our file_operations When the structure type changes , For example, add members 、 Reduction of membership 、 Adjust membership order , Then use this structure type to define a large number of variables C Files need to be re initialized , That will lead to substantial changes in the program .
The specified initializer of the structure array
After describing the above specified initializers for structures and arrays , We can also initialize the structure array in this way , Such as this :
-
#include <stdio.h>
-
int main(void)
-
{
-
struct point {int x,y;};
-
struct point pts[5] =
-
{
-
[2].y = 5,
-
[2].x = 6,
-
[0].x = 2
-
};
-
int i;
-
for(i = 0;i < 5;i++)
-
printf("%d %d\n",pts[i].x,pts[i].y);
-
}
The output is as follows :
-
2 0
-
0 0
-
6 5
-
0 0
-
0 0
summary
The above is the general content of the specified initializer , This is also the blind spot of my previous knowledge , Through this summary, learn , Can also be a good grasp of , Short step , A thousand miles ~
Reference material :
[1] https://blog.51cto.com/zhaixue/2346825
[2] https://www.geeksforgeeks.org/designated-initializers-c/
[3] https://aticleworld.com/state-machine-using-c/
Your reading is my greatest encouragement , Your suggestion is the biggest promotion for me
版权声明
本文为[Traceless rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230143119678.html
边栏推荐
- Jerry's AI server [chapter]
- Summary of commonly used commands of LSF
- MySQL basic record
- Encrypted compressed backup bat script
- FL studio20. 8 the latest Chinese version installation and download graphic tutorial
- Blocking granularity of gbase 8s concurrency control
- 紫光国微财报一枝独秀 2021年净利润三位数增长靠什么
- W801/W800-wifi-socket开发(二)-UDP蓝牙控制wifi连接
- 2022.4.10-----leetcode. eight hundred and four
- How to change the size of SVG pictures without width in openlayer
猜你喜欢
2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track
最长公共子序列(记录路径版)
How to change the size of SVG pictures without width in openlayer
四级城市地区表 xlsx, sql文件,国内,中国省市县街道乡镇四级地址 (名称,联动ID,层级,是否末级(1-是))
LSF的使用方法总结
电子采购如何成为供应链中的增值功能?
无关联进程间通信 —— 命名管道的创建和使用
力扣(LeetCode)112. 路径总和(2022.04.22)
DFS parity pruning
Dimension C China helping farmers in rural areas warms people's hearts the third stop is jiabaoguo farm
随机推荐
Summary of commonly used commands of LSF
Jerry's AI server [chapter]
iTextSharp 页面设置
Level 4 city area table xlsx, SQL file, domestic, provincial, county, street, township level 4 address in China (name, linkage ID, level, end level or not (1-yes))
. net unit test Part 1: common Net unit test framework?
mb_substr()、mb_strpos()函数(故事篇)
leetcode771. Gemstones and stones
W801 / w800 WiFi socket development (II) - UDP Bluetooth control WiFi connection
Introduction to PCIe xdma IP core (with list) - mingdeyang science and Education (mdy edu. Com)
The most understandable life cycle of dependency injection
JSP基础知识总结
腾讯云接口进行人脸检测 和签名出错问题
DO447管理用户和团队的访问
Counting garlic customers: Sudoku (DFS)
2022 low voltage electrician examination questions and answers
LSF的使用方法总结
Sqlserver data transfer to MySQL
Is the stable currency a super opportunity to accelerate the death of the public chain or replace BTC?
Do447 manage user and team access
W801 / w800 WiFi socket development (I) - UDP