zoukankan      html  css  js  c++  java
  • C语言结构体

     1 #include <stdio.h>
     2  
     3 struct student_st
     4 {
     5     char c;
     6     int score;
     7     const char *name;
     8 };
     9  
    10 static void show_student(struct student_st *stu)
    11 {
    12     printf("c = %c, score = %d, name = %s
    ", stu->c, stu->score, stu->name);
    13 }
    14  
    15 int main(void)
    16 {
    17     // method 1: 按照成员声明的顺序初始化
    18     struct student_st s1 = {'A', 91, "Alan"};
    19     show_student(&s1);
    20  
    21     // method 2: 指定初始化,成员顺序可以不定,Linux 内核多采用此方式
    22     struct student_st s2 = 
    23     {
    24         .name = "YunYun",
    25         .c = 'B',
    26         .score = 92,
    27     };
    28     show_student(&s2);
    29  
    30     // method 3: 指定初始化,成员顺序可以不定
    31     struct student_st s3 = 
    32     {
    33         c: 'C',
    34         score: 93,
    35         name: "Wood",
    36     };
    37     show_student(&s3);
    38      
    39     return 0;
    40 }

    -----------------------------------------------------------------------------------------------------------------------------------------------------------

    结构体数组:

      struct Student{

        char *p_name;

        char *p_address;

        int  age;

      }

    1):   struct Student Students[100];

        Students[0]={"zhangsan","shanghai",19};

        Students[1]={.......................................};

        ...............................................................

    2):  在声明结构体的时候就指定数组名和大小(大小可以不填);

      

              struct Student{

                char *p_name;

                char *p_address;

                int  age;

              }Students[100];

    3):声明结构体数组就对其初始化;

    struct Student{

        char *p_name;

        char *p_address;

        int  age;

      }Students[100]={{"zhangsan","shanghai",20},{"lisi","beijing",20},{"lisa","nanjing",21}..................................};

    ------------------------------------------------------------------------------------------------------------------------------------------------------------

    对于结构体变量的定义

    1):

    struct Student{

        char *p_name;

        char *p_address;

        int  age;

      }student1,student2;

    2):

    struct Student{

        char *p_name;

        char *p_address;

        int  age;

      };

    struct Student student1={"linda","上海",20};

    struct student student2;

    student2=student1;    //相同结构体的变量可以直接复制

    --------------------------------------------------------------------------------------------------------------------------------------------------------------

    指向结构体变量的指针

    struct strudent S1;

    struct strudent *p;

    p=&S1;

    (*p)  就是相当于S1;

    假如S1.name="linda";

    则:   (*p).name="linda";

    或    p->name="linda";

    C语言为了简便:(*p).name  可以简化为 p->name;  ->  为指向运算符。

    ----------------------------------------------------------------------------------------------------------------------------------------------------------

    指向结构体数组的指针

      

    struct Student{

        char *p_name;

        char *p_address;

        int  age;

      };

    struct Student Students[3]={{....},{....},{....}};

      struct Strudent *p_student;    //定义指针

      p_student=Students ;     //把结构体变量数组名赋予该指针(数组名本身就是指针);

    for(;p_student<Students+3;p_student++){

      printf("name=%s address=%s age=%d ",p_name->name,p_name->address,p_age->age);

    }

  • 相关阅读:
    <frame>、<iframe>、<embed>、<object> 和 <applet>
    xss攻击
    回流 和 重绘
    defer 和 async 的区别
    从输入URL到浏览页面的过程
    webkit vs v8
    缓存
    LeetCode
    LeetCode
    LeetCode
  • 原文地址:https://www.cnblogs.com/coversky/p/6519759.html
Copyright © 2011-2022 走看看