免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4199 | 回复: 10
打印 上一主题 下一主题

今天看到的诡异的C语言语法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 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 }

复制代码

对应的是数组

  1. unsigned int hook_entry[NF_IP_NUMHOOKS];
复制代码


我试了一下,数组确实是可以这样来初始化的:


  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 [报告]
发表于 2009-10-29 15:50 |只看该作者
是可以这样初始化啊,你想表达什么??

论坛徽章:
0
3 [报告]
发表于 2009-10-29 15:50 |只看该作者
我试了一下,C89的标准都可以,gcc带-std=c89参数编译可以通过

论坛徽章:
0
4 [报告]
发表于 2009-10-29 15:51 |只看该作者

回复 #2 yaj114777175 的帖子

我想说我之前没有看过这样的语法...

论坛徽章:
0
5 [报告]
发表于 2009-10-29 15:52 |只看该作者
之前看到的,顶多是{[0]=0}这样的语法,不带等号的还真没见过....

论坛徽章:
0
6 [报告]
发表于 2009-10-29 16:02 |只看该作者
]$ gcc junk.c  -std=c89 -ansi -pedantic
junk.c: 在函数‘main’中:
junk.c:3: 警告:过时的用法,应使用‘=’来指定元素初始值
junk.c:3: 警告:过时的用法,应使用‘=’来指定元素初始值
junk.c:3: 警告:过时的用法,应使用‘=’来指定元素初始值
junk.c:3: 警告:过时的用法,应使用‘=’来指定元素初始值
junk.c:8: 警告:隐式声明与内建函数‘printf’不兼容


应该是超老的写法了

论坛徽章:
0
7 [报告]
发表于 2009-10-29 16:03 |只看该作者

回复 #6 albcamus 的帖子

哦,后面几个参数啥意思?我只写了std=89

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:45
8 [报告]
发表于 2009-10-29 16:15 |只看该作者
不带等号的确没见过~

倒是加过c99的.foo = bar;的初始化语法的。

论坛徽章:
0
9 [报告]
发表于 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 [报告]
发表于 2009-10-29 20:49 |只看该作者

回复 #7 converse 的帖子

-pedantic好像是为了警告使用gcc c扩展的选项,和-ansi或者-std=一起用,具体man一下吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP