emptyCoder

 

引自: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;
}

 

分类:

技术点:

相关文章:

  • 2021-11-15
  • 2021-10-06
  • 2021-07-26
  • 2021-12-08
  • 2021-11-05
  • 2021-11-05
  • 2021-12-08
猜你喜欢
  • 2021-11-05
  • 2022-12-23
  • 2022-12-23
  • 2021-08-14
  • 2021-10-02
  • 2021-09-19
  • 2022-12-23
相关资源
相似解决方案