龙空技术网

C语言二级必备知识,C语言结构体基本介绍。

喵喵视角 92

前言:

现时小伙伴们对“c语言结构体赋值 点成员赋值法”大致比较注重,你们都想要了解一些“c语言结构体赋值 点成员赋值法”的相关内容。那么小编在网络上收集了一些对于“c语言结构体赋值 点成员赋值法””的相关知识,希望你们能喜欢,看官们快快来了解一下吧!

结构体是一种构造数据类型。用于把不同类型数据合并成为一个整体定义:

  struct  结构体名             {                类型标识符  成员名;                类型标识符   成员名;  // 结构体类型定义的时候,                //不可以赋初值。                            }; // 不要忘记结尾的分号 

结构体的声明:

struct  stu     {         int num;        char name [30];    };

stu是结构体类型,类型不分配内存,不能赋值,不能运算,不能存取。

struct stu {    int num ;     char name[30];}  st1;

st1 是结构体变量分配内存,可以赋值,存储。

结构体变量定义的两种形式:

// 第一种: 直接在类型后面定义struct  stu{     int num;    char name [20];}  st1;   // 不要忘记后面的分号 // 第二种形式:      void main ()      {          struct stu st1 ;   // 不要忘记声明类型      }

初始化结构体变量的三种形式:

//第一种形式: struct stu {     int num;     char name[20] }st1={ 100 , "sda"};  // 第二种:   struct stu st1={1001,"sda"};    //第三种:       st1.num=100;       st1.name="sdda"; // 错误       sprintf (st1.name,sdda");       // 因为字符串是常量,不能被直接赋值

结构体用大括号赋值,只有在创建并初始化的时候才可以。

定义匿名结构体变量: 唯一的方法:

struct {    int num;    char name [30];} st1;  

结构体变量的引用:

结构体变量名 . 成员

可以将一个结构体变量赋值给另一个结构体变量。但是两个结构体变量类型必须是同一类型

struct  stu {    int num;    char name[20];    struct chid    {        int age;        char sex;    };};//结构体内部再次定义一个结构体,但是没有定义实例的时候,//再次定义的结构体内部的变量,会被当作母体结构体变量成员。
struct  stu {    int num;    char name[20];    struct chid    {        int age;        char sex;    } hha={....};}st1;

结构体数组:

定义:struct student{    int num;    char name[];};struct student stu[] ={ {100,"aaa"},                        {101,"asd"},                        {102,"asd"}                        }; struct student stu[] ={ 100,"s",102,"sa",103,"sad"};    // 也可以挨个的赋值。 但是匿名结构体不可以挨个的赋值。  

结构体指针:

一,定义:  struct 结构体名 *结构体指针名 (:存放结构体变量的起始地址)   指向结构体变量数组的指针:   struct students    {       int num;       char name[20];   };  struct students stu[3] ={ {100,"aaa"},                          {101,"asd"},                          {102,"asd"}                          };     struct students stu *p= stu;                         二,引用:         1.  用成员名引用:  stu[0].num; stu[i].name    2.  指针引用: *p.num  ;    *(p+1) .name                   p->num ;    (P+1)-> name     3. 指针循环:       for (;p<stu+n;p++)       {            p->num;           p->name;       }               

标签: #c语言结构体赋值 点成员赋值法