The profoundness of C language: assignment operations of arrays and structures

Publisher:SereneGardenerLatest update time:2015-05-27 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
First, let's talk about array assignment. Well, in C language, array assignment and structure assignment are very simple, just use = and memcpy operations.
 
But recently, when I was looking at the kernel implementation code for queue scheduling, I found the following strange assignment statement. (For the specific program, see line 539 of linux-2.6.8:net/core/Rtnetlink.c)
  1. #define RTM_MAX (RTM_BASE+47)
  2. #define RTM_GETLINK (RTM_BASE+2)
     

  3.  
  4. struct rtnetlink_link
     
  5. {
     
  6.     int (*doit)(struct sk_buff *, struct nlmsghdr*, void *attr);
     
  7.     int (*dumpit)(struct sk_buff *, struct netlink_callback *cb);
     
  8. };
     

  9.  
  10. static struct rtnetlink_link link_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
     
  11.     //RTM_MAX-RTM_BASE+1=48

  12.  
  13. {
     
  14.     [RTM_GETLINK - RTM_BASE] = { .dumpit = rtnetlink_dump_ifinfo },
     
  15.     [RTM_SETLINK - RTM_BASE] = { .doit = ​​do_setlink },
     
  16.     [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
     
  17.     [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
     
  18.     [RTM_NEWNEIGH - RTM_BASE] = { .doit = ​​neigh_add },
     
  19.     [RTM_DELNEIGH - RTM_BASE] = { .doit = ​​neigh_delete },
     
  20.     [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info }
     
  21. };
  This is the first time I have seen the assignment statement above. After reading it for a long time, I finally understood the meaning of this assignment statement. It turns out that C language can also assign values ​​in this way.
  Note: There are two things to note about the above assignment statement.
  1, [RTM_GETLINK - RTM_BASE] = { .dumpit = rtnetlink_dump_ifinfo }, This indicates that when assigning values ​​to an array, the C language can assign values ​​to the element in question. In this example, it is equal to [2] = {...}, which means assigning values ​​to the second element.
  2. { .dumpit = rtnetlink_dump_ifinfo } indicates that, in the structure assignment, a specific structure member can also be assigned separately.
Reference address:The profoundness of C language: assignment operations of arrays and structures

Previous article:Understanding the topology of high voltage power supply (1)
Next article:Content and number of bits of a TCP/IP frame

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号