• 定义结构体数组的一般形式是

struct 结构体名{成员列表}数组名[数组长度];

struct Student
{
    char name[20];
    int count;
}leader[3] = {"Li",0,"zhang",0,"sun",0 };

 

  • 先声明一个结构体类型,然后再用此类型定义结构体数组
struct  person leader[3];

 

 

  • 结构体数组初始化
struct  person leader[3] = {"Li",0,"Ming",0,"Gang",0};

 

 

结构体指针

1.指向结构体对象的指针变量既可以指向结构体变量,也可以指向结构体数组中的元素

 

struct Student *pt;

 

 

 

 

应用:

struct Student
{
    int num;
    char name[20];
    char sex;

};
struct Student student1;
struct Student *p;
p = &student1;//取地址符
printf("%d", student1.num);
printf("%d", (*p).num);//输出相同

为了直观和方便,C语言允许把(*p).num用p->num来替代

2.指向结构体数组的指针

 

include<stdio.h>;
struct Studnet
{
    int mum;
};
struct student stu[3] = {2,1,1};
int main
{
    struct Student *p;
for (p = stu; p < stu + 3; p++)
{
    cout << p->num;
}
return 0;
}

 

 

 

 

3.用结构体变量和结构体变量的指针作为函数参数

将一个结构体变量的值传递给另一个函数,有三个办法

(1)用结构体变量的成员作参数

例如stu[1].num作函数实参

(2)用结构体变量做实参,形参也必须是同类型的结构体变量

(3)用指向结构体变量的指针作实参;

#include<stdio.h>;
struct Studnet
{
    int mum;
};
struct student stu[3] = {2,1,1};

void input(struct Student stu[])
{

}
int main
{
    struct Student *p;
    *p = stu;
    intput(p);//(3)

    return 0;
}

 

 

 

 

 

 

相关文章:

  • 2022-12-23
  • 2021-06-05
  • 2021-08-22
  • 2021-11-26
  • 2021-12-13
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-22
  • 2021-11-30
  • 2021-11-09
相关资源
相似解决方案