当前位置 博文首页 > 胡文燕的博客:C语言struct 函数,c语言struct的用法

    胡文燕的博客:C语言struct 函数,c语言struct的用法

    作者:[db:作者] 时间:2021-09-17 18:17

    C语言中先申明结构体,也就是定义结构体具体形式,而后可以把它当做普通数据类型来修饰变量,也可以在定义类型时后面直接跟着定义几个变量,下面小编就为你介绍c语言struct的用法。

    基本定义:结构体,通俗讲就像是打包封装,把一些有共同特征(比如同属于某一类事物的属性,往往是某种业务相关属性的聚合)的变量封装在内部,通过一定方法访问修改内部变量。

    结构体定义:

    第一种:只有结构体定义

    [cpp] view plain

    structstuff{

    charjob[20];

    intage;

    floatheight;

    };

    第二种:附加该结构体类型的“结构体变量”的初始化的结构体定义

    [cpp] view plain

    //直接带变量名Huqinwei

    structstuff{

    charjob[20];

    intage;

    floatheight;

    }Huqinwei;

    也许初期看不习惯容易困惑,其实这就相当于:

    [cpp] view plain

    structstuff{

    charjob[20];

    intage;

    floatheight;

    };

    structstuffHuqinwei;

    第三种:如果该结构体你只用一个变量Huqinwei,而不再需要用

    [cpp] view plain

    structstuffyourname;

    去定义第二个变量。

    那么,附加变量初始化的结构体定义还可进一步简化出第三种:

    [cpp] view plain

    struct{

    charjob[20];

    intage;

    floatheight;

    }Huqinwei;

    把结构体名称去掉,这样更简洁,不过也不能定义其他同结构体变量了——至少我现在没掌握这种方法。

    结构体变量及其内部成员变量的定义及访问:

    绕口吧?要分清结构体变量和结构体内部成员变量的概念。

    就像刚才的第二种提到的,结构体变量的声明可以用:

    [cpp] view plain

    structstuffyourname;

    其成员变量的定义可以随声明进行:

    [cpp] view plain

    structstuffHuqinwei={"manager",30,185};

    也可以考虑结构体之间的赋值:

    [cpp] view plain

    structstufffaker=Huqinwei;

    //或structstufffaker2;

    //faker2=faker;

    打印,可见结构体的每一个成员变量一模一样

    如果不使用上边两种方法,那么成员数组的操作会稍微麻烦(用for循环可能好点)

    [cpp] view plain

    Huqinwei.job[0]='M';

    Huqinwei.job[1]='a';

    Huqinwei.age=27;

    nbsp;Huqinwei.height=185;

    结构体成员变量的访问除了可以借助符号".",还可以用"->"访问(下边会提)。

    指针和数组:

    这是永远绕不开的话题,首先是引用:

    [cpp] view plain

    structstuff*ref=&Huqinwei;

    ref->age=100;

    printf("ageis:%d\n",Huqinwei.age);

    指针也是一样的

    [cpp] view plain

    structstuff*ptr;

    ptr->age=200;

    printf("ageis:%d\n",Huqinwei.age);

    结构体也不能免俗,必须有数组:

    [cpp] view plain

    structtest{

    inta[3];

    intb;

    };

    //对于数组和变量同时存在的情况,有如下定义方法:

    structteststudent[3]={{{66,77,55},0},

    {{44,65,33},0},

    {{46,99,77},0}};

    //特别的,可以简化成:

    structteststudent[3]={{66,77,55,0},

    {44,65,33,0},

    {46,99,77,0}};

    变长结构体

    可以变长的数组

    [cpp] view plain

    #include

    #include

    #include

    typedefstructchangeable{

    intiCnt;

    charpc[0];

    }schangeable;

    main(){

    printf("sizeofstructchangeable:%d\n",sizeof(schangeable));

    schangeable*pchangeable=(schangeable*)malloc(sizeof(schangeable)+10*sizeof(char));

    printf("sizeofpchangeable:%d\n",sizeof(pchangeable));

    schangeable*pchangeable2=(schangeable*)malloc(sizeof(schangeable)+20*sizeof(char));

    pchangeable2->iCnt=20;

    printf("pchangeable2->iCnt:%d\n",pchangeable2->iCnt);

    strncpy(pchangeable2->pc,"helloworld",11);

    printf("%s\n",pchangeable2->pc);

    printf("sizeofpchangeable2:%d\n",sizeof(pchangeable2));

    }

    运行结果

    [cpp] view plain

    sizeofstructchangeable:4

    sizeofpchangeable:4

    pchangeable2->iCnt:20

    helloworld

    sizeofpchangeable2:4

    结构体本身长度就是一个int长度(这个int值通常只为了表示后边的数组长度),后边的数组长度不计算在内,但是该数组可以直接使用。

    (说后边是个指针吧?指针也占长度!这个是不占的!原理很简单,这个东西完全是数组后边的尾巴,malloc开辟的是一片连续空间。其实这不应该算一个机制,感觉应该更像一个技巧吧)

    结构体嵌套:

    结构体嵌套其实没有太意外的东西,只要遵循一定规律即可:

    [cpp] view plain

    //对于“一锤子买卖”,只对最终的结构体变量感兴趣,其中A、B也可删,不过最好带着

    structA{

    structB{

    intc;

    }

    b;

    }

    a;

    //使用如下方式访问:

    a.b.c=10;

    特别的,可以一边定义结构体B,一边就使用上:

    [cpp] view plain

    structA{

    structB{

    intc;

    }b;

    structBsb;

    }a;

    使用方法与测试:

    [cpp] view plain

    a.b.c=11;

    printf("%d\n",a.b.c);

    a.sb.c=22;

    printf("%d\n",a.sb.c);

    结果无误。

    结构体与函数:

    关于传参,首先:

    [cpp] view plain

    voidfunc(int);

    func(a.b.c);

    把结构体中的int成员变量当做和普通int变量一样的东西来使用,是不用脑子就想到的一种方法。

    另外两种就是传递副本和指针了 :

    [cpp] view plain

    //structA定义同上

    //设立了两个函数,分别传递structA结构体和其指针。

    voidfunc1(structAa){

    printf("%d\n",a.b.c);

    }

    voidfunc2(structA*a){

    printf("%d\n",a->b.c);

    }

    main(){

    a.b.c=112;

    structA*pa;

    pa=&a;

    func1(a);

    func2(&a);

    func2(pa);

    }

    占用内存空间:

    struct结构体,在结构体定义的时候不能申请内存空间,不过如果是结构体变量,声明的时候就可以分配——两者关系就像C++的类与对象,对象才分配内存(不过严格讲,作为代码段,结构体定义部分“.text”真的就不占空间了么?当然,这是另外一个范畴的话题)。

    结构体的大小是结构体所含变量大小的总和,并且不能用"char a[]"这种弹性(flexible)变量,必须明确大小,下面打印输出上述结构体的size:

    [cpp] view plain

    printf("sizeofstructman:%d\n",sizeof(structman));

    printf("size:%d\n",sizeof(Huqinwei));

    结果毫无悬念,都是28:分别是char数组20,int变量4,浮点变量4.

    和C++的类不一样,结构体不可以给结构体内部变量初始化,。

    如下,为错误示范:

    [cpp] view plain

    #include

    //直接带变量名Huqinwei

    structstuff{

    //charjob[20]="Programmer";

    //charjob[];

    //intage=27;

    //floatheight=185;

    }Huqinwei;

    PS:结构体的声明也要注意位置的,作用域不一样。

    C++的结构体变量的声明定义和C有略微不同,说白了就是更“面向对象”风格化,要求更低。

    cs