【结构体】

简介: 【结构体】

初阶结构体

结构体关键字----struct

  1. 描述一个学生-----一些数据 :
    名字
    年龄
    电话
    性别

(1)第一种写法,Stu 结构体标签 ,struct Stu - 结构体类型

struct Stu                
  {
    //成员变量
    char name[20];
    short age;            //定义一个结构体类型     相当于int
    char tele[12];
    char sex[5];
  };//s1,s2,s3;       //s1,s2,s3是三个全局结构体变量(一般不用)
  int main()
  {
    struct Stu s;        //创建结构体变量(局部变量)                相当于int a = 10;
    return 0;
  }

(2)第二种写法, 使用typedef关键字把struct Stu重新起名字为Stu,在;前起新的名字

typedef struct Stu           
  {
    char name[20];
    short age;
    char tele[12];
    char sex[5];
  }Stu;                               //Stu:类型
  int main()
  {
    Stu s1 = { "张三",30,"15236985412","男" };            //初始化变量,结构体初识化要用{} 
    Stu s2 = { "李四",20,"15421410451","男" };
    printf("%s %d ", s1.name, s1.age);                   //结构体变量.成员
    printf("\n");
    Stu* ps = &s1;
    printf("%s %d ", ps->name, ps->age);                //结构体指针->成员
    return 0;
  }

(3)第三种写法

struct S
  {
    int a;
    char c;
    char arr[20];
    double d;
  };
  struct T
  {
    char ch[10];
    struct S s;
    char* pc;
  };
  int main()
  {
    char arr[] = "hello bit\n";
    struct T t = { "hehe",{220,'a',"hello,world",3.14},arr };
    printf("%s\n", t.ch);
    printf("%s\n", t.s.arr);
    printf("%lf\n", t.s.d);
    printf("%s\n", t.pc);
    return 0;
  }
目录
相关文章
|
2月前
|
编译器 Linux C语言
结构体(详解)
结构体(详解)
26 1
|
5月前
|
算法 程序员 C++
|
5月前
|
C语言
使用结构体
C 语言实例 - 使用结构体。
104 4
|
27天前
|
Java 编译器 Linux
再次认识结构体
再次认识结构体
41 0
|
6月前
|
机器学习/深度学习 存储 编译器
Day_16 结构体
Day_16 结构体
|
9月前
|
编译器
|
10月前
|
C#
C#视频-结构体
C#视频-结构体
40 0
|
10月前
初识结构体
初识结构体
|
11月前
|
存储 C++