结构体

结构体,第1张

//结构体
//char int double float .. 
// 人 = 3.14;
//'w'
// 书 - 复杂对象
//
// 名字+身高——年龄+身份z...
//书名+作者+出版社。。
//  复杂对象 - 结构体 - 创造出来的类型
//
//创建一个结构体类型
struct Book
{
    char name[20];//C语言程序设计
    short price;//55
};

int main()
{
    //利用结构体类型-创建一个该类型的结构体变量
    struct Book b1 = { "C语言程序设计", 55 };
    struct Book* pb = &b1;
    //利用pb打印出我的书名和价格
    //.  结构体变量.成员
    //-> 结构体指针->成员
    printf("书名:%s\n", pb->name);
    printf("价格:%d\n", pb->price);


    printf("书名:%s\n", (*pb).name);
    printf("价格:%d\n", (*pb).price);
    
    strcpy(b1.name, "c++");//strcpy - string copy - 字符串拷贝-库函数 string.h

    printf("修改后书名:%s\n", b1.name);
    printf("价格:%d\n", b1.price);
    b1.price = 15;
    
    printf("修改后的价格:%d\n", b1.price);
    
    system("pause");
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/713573.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-24
下一篇 2022-04-24

发表评论

登录后才能评论

评论列表(0条)

保存