# 结构体

1、结构体变量的初始化

```#include "StdAfx.h"
#include<stdio.h>

void main()
{
struct
{
int num;
char *name;
char sex;
float score;
}boy1,boy2={102,"Jane",‘M‘,98.5};
boy1=boy2;
printf("number=%d\nname=%s\nscore=%f\n",boy1.num,boy1.name,boy1.score);
printf("number=%d\nname=%s\nscore=%f\n",boy2.num,boy2.name,boy2.score);
}```

2、结构体数组

```#include "StdAfx.h"
#include<stdio.h>
#include<stdlib.h>
#define NUM 3
struct person
{
char name[20];
char phone[10];
};
void main()
{
struct person man[NUM];
int i;
for(i=0;i<NUM;i++)
{
printf("input name:\n");
gets(man[i].name);
printf("input phone:\n");
gets(man[i].phone);
}
printf("\tname\t\t\tphone\n\n\n");
for(i=0;i<NUM;i++)
{
printf("%10s\t\t%10s\n",man[i].name,man[i].phone);
}
system("pause");
}```

```struct student
{
int num;
char name[20];
char sex;
int age;
float score;
}stu[2] = {
{101,"LILIN","m",18,97.5,"beijing"},
{102,"zhang","f",17,99,"shanghai"}
};```

```struct student
{
int num;
char name[20];
char sex;
int age;
float score;
};
struct student str[2] = {
{101,"LILIN","m",18,97.5,"beijing"},
{102,"zhang","f",17,99,"shanghai"}
};```

3、指向结构i数据类型的指针

```#include "StdAfx.h"
#include<stdio.h>
struct stu
{
int num;
char *name;
char sex;
float score;
}boy1={102,"kun",‘m‘,98.5};
void main()
{
struct stu *pstu;
pstu=&boy1;
printf("number=%d\nname=%s\nsex=%c\nscore=%f\n",boy1.num,boy1.name,boy1.sex,boy1.score);
printf("number=%d\nname=%s\nsex=%c\nscore=%f\n",(*pstu).num,(*pstu).name,(*pstu).sex,(*pstu).score);
}```

(0)
(0)