亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
12下一頁
最近訪問板塊 發(fā)新帖
查看: 4238 | 回復: 10
打印 上一主題 下一主題

今天看到的詭異的C語言語法 [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2009-10-29 15:47 |只看該作者 |倒序瀏覽
今天看內核中neitfilter部分的代碼,在iptable_filter.c中有這樣的變量:


  1. static struct
  2. {
  3. struct ipt_replace repl;
  4. struct ipt_standard entries[3];
  5. struct ipt_error term;
  6. } initial_table __initdata
  7. = { { "filter", FILTER_VALID_HOOKS, 4,
  8.       sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
  9.       { [NF_IP_LOCAL_IN] 0,
  10. [NF_IP_FORWARD] sizeof(struct ipt_standard),
  11. [NF_IP_LOCAL_OUT] sizeof(struct ipt_standard) * 2 },
  12.       { [NF_IP_LOCAL_IN] 0,
  13. [NF_IP_FORWARD] sizeof(struct ipt_standard),
  14. [NF_IP_LOCAL_OUT] sizeof(struct ipt_standard) * 2 },
  15.       0, NULL, { } },
  16.     {
  17.      /* LOCAL_IN */
  18.      { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  19.   0,
  20.   sizeof(struct ipt_entry),
  21.   sizeof(struct ipt_standard),
  22.   0, { 0, 0 }, { } },
  23.        { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  24.   -NF_ACCEPT - 1 } },
  25.      /* FORWARD */
  26.      { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  27.   0,
  28.   sizeof(struct ipt_entry),
  29.   sizeof(struct ipt_standard),
  30.   0, { 0, 0 }, { } },
  31.        { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  32.   -NF_ACCEPT - 1 } },
  33.      /* LOCAL_OUT */
  34.      { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  35.   0,
  36.   sizeof(struct ipt_entry),
  37.   sizeof(struct ipt_standard),
  38.   0, { 0, 0 }, { } },
  39.        { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
  40.   -NF_ACCEPT - 1 } }
  41.     },
  42.     /* ERROR */
  43.     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
  44. 0,
  45. sizeof(struct ipt_entry),
  46. sizeof(struct ipt_error),
  47. 0, { 0, 0 }, { } },
  48.       { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
  49.    { } },
  50. "ERROR"
  51.       }
  52.     }
  53. };


復制代碼

其中的結構體定義struct ipt_replace
是:


  1. /* The argument to IPT_SO_SET_REPLACE. */
  2. struct ipt_replace
  3. {
  4. /* Which table. */
  5. char name[IPT_TABLE_MAXNAMELEN];

  6. /* Which hook entry points are valid: bitmask.  You can't
  7.            change this. */
  8. unsigned int valid_hooks;

  9. /* Number of entries */
  10. unsigned int num_entries;

  11. /* Total size of new entries */
  12. unsigned int size;

  13. /* Hook entry points. */
  14. unsigned int hook_entry[NF_IP_NUMHOOKS];

  15. /* Underflow points. */
  16. unsigned int underflow[NF_IP_NUMHOOKS];

  17. /* Information about old entries: */
  18. /* Number of counters (must be equal to current number of entries). */
  19. unsigned int num_counters;
  20. /* The old entries' counters. */
  21. struct ipt_counters *counters;

  22. /* The entries (hang off end: not really an array). */
  23. struct ipt_entry entries[0];
  24. };

復制代碼

他對應的前面這一大段代碼:

  1. { "filter", FILTER_VALID_HOOKS, 4,
  2.       sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
  3.       { [NF_IP_LOCAL_IN] 0,
  4. [NF_IP_FORWARD] sizeof(struct ipt_standard),
  5. [NF_IP_LOCAL_OUT] sizeof(struct ipt_standard) * 2 },
  6.       { [NF_IP_LOCAL_IN] 0,
  7. [NF_IP_FORWARD] sizeof(struct ipt_standard),
  8. [NF_IP_LOCAL_OUT] sizeof(struct ipt_standard) * 2 },
  9.       0, NULL, { } }

復制代碼


關鍵在于:

  1. { [NF_IP_LOCAL_IN] 0,
  2. [NF_IP_FORWARD] sizeof(struct ipt_standard),
  3. [NF_IP_LOCAL_OUT] sizeof(struct ipt_standard) * 2 }

復制代碼

對應的是數(shù)組

  1. unsigned int hook_entry[NF_IP_NUMHOOKS];
復制代碼


我試了一下,數(shù)組確實是可以這樣來初始化的:


  1. int main(int argc, char *argv[])
  2. {
  3. int array[5] = {[3]1, [1]1 , [2] 2, [4] 4};
  4. int i;

  5. for (i = 0; i < 5; ++i)
  6. {
  7.   printf("array = %d\n", array[i]);
  8. }

  9. return 0;
  10. }

復制代碼

論壇徽章:
0
2 [報告]
發(fā)表于 2009-10-29 15:50 |只看該作者
是可以這樣初始化啊,你想表達什么??

論壇徽章:
0
3 [報告]
發(fā)表于 2009-10-29 15:50 |只看該作者
我試了一下,C89的標準都可以,gcc帶-std=c89參數(shù)編譯可以通過

論壇徽章:
0
4 [報告]
發(fā)表于 2009-10-29 15:51 |只看該作者

回復 #2 yaj114777175 的帖子

我想說我之前沒有看過這樣的語法...

論壇徽章:
0
5 [報告]
發(fā)表于 2009-10-29 15:52 |只看該作者
之前看到的,頂多是{[0]=0}這樣的語法,不帶等號的還真沒見過....

論壇徽章:
0
6 [報告]
發(fā)表于 2009-10-29 16:02 |只看該作者
]$ gcc junk.c  -std=c89 -ansi -pedantic
junk.c: 在函數(shù)‘main’中:
junk.c:3: 警告:過時的用法,應使用‘=’來指定元素初始值
junk.c:3: 警告:過時的用法,應使用‘=’來指定元素初始值
junk.c:3: 警告:過時的用法,應使用‘=’來指定元素初始值
junk.c:3: 警告:過時的用法,應使用‘=’來指定元素初始值
junk.c:8: 警告:隱式聲明與內建函數(shù)‘printf’不兼容


應該是超老的寫法了

論壇徽章:
0
7 [報告]
發(fā)表于 2009-10-29 16:03 |只看該作者

回復 #6 albcamus 的帖子

哦,后面幾個參數(shù)啥意思?我只寫了std=89

論壇徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:45
8 [報告]
發(fā)表于 2009-10-29 16:15 |只看該作者
不帶等號的確沒見過~

倒是加過c99的.foo = bar;的初始化語法的。

論壇徽章:
0
9 [報告]
發(fā)表于 2009-10-29 17:23 |只看該作者
是很詭異。查了一下,是 gnu 的擴展:

Using and Porting the GNU Compiler Collection (GCC)

This manual documents how to run, install and port the GNU compiler, as well as its new features and incompatibilities, and how to report bugs. It corresponds to GCC version 2.95.


4.20 Labeled Elements in Initializers

Standard C requires the elements of an initializer to appear in a fixed order, the same as the order of the elements in the array or structure being initialized.

In GNU C you can give the elements in any order, specifying the array indices or structure field names they apply to. This extension is not implemented in GNU C++.

To specify an array index, write `[index]' or `[index] =' before the element value. For example,

       

int a[6] = { [4] 29, [2] = 15 };

is equivalent to

       

int a[6] = { 0, 0, 15, 0, 29, 0 };

論壇徽章:
0
10 [報告]
發(fā)表于 2009-10-29 20:49 |只看該作者

回復 #7 converse 的帖子

-pedantic好像是為了警告使用gcc c擴展的選項,和-ansi或者-std=一起用,具體man一下吧
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP