V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Sherlockhlt
V2EX  ›  C

这是C的语法还是Linux的宏?

  •  
  •   Sherlockhlt · Jan 20, 2013 · 3045 views
    This topic created in 4849 days ago, the information mentioned may be changed or developed.
    在看Linux内核相关的时候经常看到像这样子的结构体赋值:
    struct fileOperation fops = {
    .read = read,
    .open = open,
    };
    这个语法好奇怪,在其他C程序中都没有见到过,是C的语法还是Linux的宏?
    22 replies    1970-01-01 08:00:00 +08:00
    sNullp
        1
    sNullp  
       Jan 20, 2013
    是gcc特有的语法应该。
    Sherlockhlt
        3
    Sherlockhlt  
    OP
       Jan 20, 2013
    @gDD
    原来是C99语法,不过这种写法是应该避免还是应该使用?
    gDD
        4
    gDD  
       Jan 20, 2013
    @Sherlockhlt 说不好,我不是搞C的,既然你说Linux内核都在用,没有理由不用啊,看着多清晰。
    sinxccc
        5
    sinxccc  
       Jan 20, 2013
    @Sherlockhlt 归根结底还是一句套话:视项目视团队而定。
    liuw
        6
    liuw  
       Jan 20, 2013
    这么好的东西为什么不用?
    ForgotFun
        7
    ForgotFun  
       Jan 20, 2013
    这是GCC的扩展。是内核中常用的赋值方法。
    ForgotFun
        8
    ForgotFun  
       Jan 20, 2013
    #include <stdio.h>
    #include <string.h>

    void main (int argc, char **argv)
    {
    int size = atoi(argv[1]);

    char buffer[size];

    printf("sizeof buffer %d\n", sizeof(buffer));
    }


    在GCC中这程序也是合法。
    wog
        9
    wog  
       Jan 20, 2013
    @ForgotFun 开玩笑吧,void main 在哪里都不合法
    Sherlockhlt
        10
    Sherlockhlt  
    OP
       Jan 20, 2013
    @liuw
    主要是怕兼容性不好
    ForgotFun
        11
    ForgotFun  
       Jan 20, 2013
    @wog
    int size = atoi(argv[1]);

    char buffer[size];

    这里两句才是重点。
    Mutoo
        12
    Mutoo  
       Jan 20, 2013
    @wog void main 在单片机里可以有。
    sivacohan
        13
    sivacohan  
    PRO
       Jan 20, 2013 via Android
    没看明白重点。。。
    有时候我也这么写啊。
    kneep
        14
    kneep  
       Jan 20, 2013 via iPhone
    这是GNU C的扩展Linux里面大量存在
    fangzhzh
        15
    fangzhzh  
       Jan 20, 2013   ❤️ 1
    @sivacohan 骚年, 静态数组长度是变量. 以前的编译器,是编不过的^_^
    dndx
        16
    dndx  
       Jan 20, 2013
    @fangzhzh
    @ForgotFun 这跟 GCC 有什么关系,VLA 是 C99 标准定义的。

    https://en.wikipedia.org/wiki/Variable-length_array
    ForgotFun
        17
    ForgotFun  
       Jan 20, 2013
    @dndx GCC 支持这个特性的时候,C99还没出来呢。
    sivacohan
        18
    sivacohan  
    PRO
       Jan 20, 2013
    @fangzhzh 完了,C忘没了……
    ssword
        19
    ssword  
       Jan 20, 2013
    C99的语法
    liuw
        20
    liuw  
       Jan 20, 2013
    @Sherlockhlt

    C99标准,编译器支持就没问题了。
    BOYPT
        21
    BOYPT  
       Jan 21, 2013
    @ForgotFun 这些标准都是先出草案再正式标准的,gcc一般在出草案时候就实现了,比如现在的C++0x/C++11,都是gcc首先实现的。
    faywong
        22
    faywong  
       Jan 21, 2013
    Linux代码的很多特性都必须由gcc来支持,并且gcc在支持c语言最新标准上最及时。所以建议在Linux平台尽情使用。若是跨平台的项目/兼容过去的陈旧代码,请尽量不要使用。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2497 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 158ms · UTC 08:31 · PVG 16:31 · LAX 01:31 · JFK 04:31
    ♥ Do have faith in what you're doing.