美文网首页C语言
C语言中结构变量的指定初值

C语言中结构变量的指定初值

作者: TortoiseY | 来源:发表于2017-05-20 21:38 被阅读0次

    结构变量指定初值的两种方式
    第一种:
    当函数原型为

    void fun(struct point point_1,struct point point_2);
    

    结构变量初始化使用这种

    #include<stdio.h>
    struct point{
      int x;
      int y;
    }point_1,point_2;
    void fun(struct point point_1,struct point point_2);
    int main(void){
      fun((struct point_1){.x=1,.y=2},(struct point _2){.x=3,.y=4} )
      ..........
      ..........
    }
    

    第二种:
    当函数原型为

    void fun(struct point *pa,struct point *pb);
    

    结构变量初始化使用这种

    #include<stdio.h>
    struct point{
      int x;
      int y;
    }point_1,point_2;
    void fun(struct point *pa,struct point *pb);
    int main(void){
      fun(&(struct point_1){.x=1,.y=2},&(struct point_2){.x=3,.y=4} )
      ..........
      ..........
    }
    

    C语言初始化语法更加详细的BNF描述和例子可以参考ISO/IEC 9899:2011,p139.

    相关文章

      网友评论

        本文标题:C语言中结构变量的指定初值

        本文链接:https://www.haomeiwen.com/subject/ksnpxxtx.html