c语言结构体小知识

 

引自:http://c.biancheng.net/cpp/html/88.html

结构体在内存中是连续存储的

struct stu{
    char *name;  //姓名
    int num;  //学号
    char sex;  //性别
    float score;  //成绩
} stu1, stu2 = { "Tom", 10, 'M', 90 };
不过整体赋值仅限于上面这样的情况,也就是定义结构体的同时声明变量。下面的写法是错误的:
stu2 = { "Tom", 10, 'M', 90 };

 

#include <stdio.h>
#define STU struct stu
int main(){
    STU{
        char *name;  //姓名
        int num;  //学号
        char sex;  //性别
        float score;  //成绩
    };
    STU stu1;
    stu1.name = "James Bond";//用指针指向一个字符串
    stu1.num = 1;
    stu1.sex = 'M';  //用M表示男性,W表示女性
    stu1.score = 99;
    printf("Hello everyone! My name is %s, a naughty boy, but with good scores(%.2f) and top No.(%d)!", stu1.name, stu1.score, stu1.num);
    return 0;
}

 

转载于:https://www.cnblogs.com/520xiuge/p/5573187.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值