其他分享
首页 > 其他分享> > 结构体

结构体

作者:互联网

一 赋值和初始化

# include <stdio.h>
struct Student//只是定义了一个数据类型,并没有定义变量
{
    int age;
    float score;
    char sex;
} ;

int main ()
{
    //类似于数组 
    struct Student st1={80,66.6,'F'};//初始化 定义的同时赋初值 
    struct Student st2;//如果定义完之后只能分开赋值 
    st2.age=10;
    st2.score=88;
    st2.sex='F'; 
    
    printf("%d %f %c\n",st1.age,st1.score,st1.sex); 
    printf("%d %f %c\n",st2.age,st2.score,st2.sex);
    
    return 0;
}

二 如何取出结构体变量中的每一个成员
1.结构体变量名.成员名
2.指针变量->成员名 这种方式更常用

# include <stdio.h>
struct Student
{
    int age;
    float score;
    char sex;
} ;

int main ()
{
    struct Student st1={80,66.6,'F'};
    st1.age;//第一种方式
    struct Student* p = &st1;//构造指针变量 
    p->age;//第二种方式 p->age在计算机内部会转换为(*p).age硬性规定 又等价于p.age 
    return 0;
}

标签:struct,age,sex,st2,st1,Student,结构
来源: https://www.cnblogs.com/panghushalu/p/11965429.html