In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
2.1 what is specified initialization
In standard C, when we define and initialize an array, the common methods are as follows:
Int a [10] = {0pm 1pm 2je 3je 4je 5pm 6je 7je 8}
In this fixed order, we can assign values to a [0] and a [8] in turn. Because there is no assignment to a [9], the compiler sets a [9] to 0 by default. When the array length is small, it is convenient to initialize in this way. When the array is large and the non-zero elements in the array are not contiguous, it is troublesome to initialize them in a fixed order.
For example, we define an array b [100], in which b [10] and b [30] need to be initialized. If it is initialized in the previous fixed order, the initialization data in {} may have to be filled with a large number of zeros, which is troublesome.
What are we going to do? The C99 standard improves the initialization of the array, supporting the initialization of specified arbitrary elements, instead of initializing in a fixed order.
Int b [10] = 1, [30] = 2}
Through the array index, we can directly assign values to the specified array elements. In addition, the initialization of a structure variable can also be assigned directly by specifying a structure domain.
Because GNU C supports the C99 standard, the GCC compiler also supports this feature. Even in the early days, C99 was not supported, only the GCC compiler version of C89, which was also provided to programmers as an extension of the GCC compiler.
2.2 specify initialization array elements
In GNU C, through the array element index, we can directly assign a value to a specified element.
Int b [10] = 1, [30] = 2}
In {}, we can directly assign values to a [10] through the element index of the [10] array. One detail to note here is that each assignment is separated by a comma, not a semicolon.
If we want to initialize the array elements of an index range in the array, we can do the following.
Int main (void) {int b [100] = {[10... 30] = 1, [50... 60] = 2}; for (int I = 0; I < 100; iTunes +) {printf ("% d", a [I]); if (I% 10 = = 0) printf ("\ n");} return 0;}
In this program, we use [10. 30] to represent an index range, which is equivalent to assigning 1 to 20 array elements between a [10] and a [30].
GNU C supports the use of... Represents range extension, which can be used not only in array initialization, but also in switch-case statements. For example, the following program:
# includeint main (void) {int I = 4; switch (I) {case 1: printf ("1\ n"); break; case 2... 8: printf ("% d\ n", I); break; case 9: printf ("9\ n"); break Default: printf ("default!\ n"); break;} return 0;}
In this program, when the case value is 2 to 8, the same case branch is executed, and the code can be simplified in the form of case 2. 8:. There is also a detail to pay attention to here, that is. And its two ends of the data range 2 and 8 should also be a space, can not be written in the form of 2. 8, otherwise the compiler will fail.
2.3 specify initialization structure member variables
Similar to arrays, in standard C, structure variables are initialized in a fixed order. In GNU C, we can also initialize and specify a member through a domain.
Struct student {char name [20]; int age;}; int main (void) {struct student stu1= {"wit", 20}; printf ("% SSV% d\ n", stu1.name,stu1.age); struct student stu2= {.name = "wanglitao", .age = 28}; printf ("% SGH% d\ n", stu2.name,stu2.age); return 0;}
In the program, we define a structure type student, and then define two structure variables stu1 and stu2, respectively. When initializing stu1, we use the standard C initialization mode, that is, direct initialization in a fixed order. When initializing stu2, we use GNU C initialization method, through the structure domain name .name and .age, we can directly assign a value to a specified member of the structure variable. It's very convenient.
2.4 Linux kernel driver registration
In the Linux kernel driver, this designated initialization mode of GNU C is widely used to initialize structure variables through structure members. For example, in character drivers, we often see initialization like this:
Static const struct file_operations ab3100_otp_operations = {.open = ab3100_otp_open,.read = seq_read,.llseek = seq_lseek,.release = single_release,}
In the driver, we often use file_operations as a structural variable to register the driver we developed, and then perform the relevant functions of our driver implementation in a callback way. The structure file_operations is defined in the Linux kernel as follows:
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 *); ssize_t (* read_iter) (struct kiocb *, struct iov_iter *) Ssize_t (* write_iter) (struct kiocb *, struct iov_iter *); int (* iterate) (struct file *, struct dir_context *); unsigned int (* poll) (struct file *, struct poll_table_struct *); long (* unlocked_ioctl) (struct file *, unsigned int, unsigned long); long (* compat_ioctl) (struct file *, unsigned int, unsigned long) Int (* mmap) (struct file *, struct vm_area_struct *); int (* open) (struct inode *, struct file *); int (* flush) (struct file *, fl_owner_t id); int (* release) (struct inode *, struct file *); int (* fsync) (struct file *, loff_t, loff_t, int datasync) Int (* aio_fsync) (struct kiocb *, int datasync); int (* fasync) (int, struct file *, int); int (* lock) (struct file *, int, struct file_lock *); ssize_t (* sendpage) (struct file *, struct page *, int, size_t, loff_t *, int) Unsigned long (* get_unmapped_area) (struct file *, unsigned long, unsigned long); int (* check_flags) (int); int (* flock) (struct file *, int, struct file_lock *); ssize_t (* splice_write) (struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int) Ssize_t (* splice_read) (struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int); int (* setlease) (struct file *, long, struct file_lock * *, void * *); long (* fallocate) (struct file * file, int mode, loff_t offset, loff_t len) Void (* show_fdinfo) (struct seq_file * m, struct file * f); # ifndef CONFIG_MMU unsigned (* mmap_capabilities) (struct file *); # endif}
Structure file_operations defines a lot of structure members, but in this driver, we only initialize some member variables. It is very convenient to specify initialization by visiting the members of the structure.
2.5 specify the benefits of initialization
This specified initialization method is not only flexible to use, but also has the advantage that the code is easy to maintain. Especially in a large project such as the Linux kernel, when tens of thousands of files and tens of millions of code are defined and initialized using the structure type file_operations, a big problem arises: if we use standard C to assign values in a fixed order, when our file_operations structure type changes, such as adding members, reducing members, and adjusting the order of members. So a large number of C files that use this structure type to define variables need to re-adjust the initialization order and affect the whole body. Think about how terrible this is!
We can avoid this problem by specifying the initialization method. No matter how the file_operations structure type changes, adding members, reducing members, or adjusting the order of members will not affect the use of other files. With the designated initialization, I no longer have to work overtime to modify the code, and my mother no longer has to worry about us working overtime all day and not coming home for dinner.
This tutorial is adapted from the C language embedded Linux Advanced programming Video tutorial No. 05, the electronic version of the book can join the QQ group: 475504428 download, more embedded video tutorials, you can follow:
Official account of Wechat: Otaku tribe (armlinuxfun)
51CTO College-Mr. Wang Litao: http://edu.51cto.com/sd/d344f
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.