#if 1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void main()
{
#if 0
struct date
{
int month;
int day;
int year;
};
#endif
struct student
{
int num;
char* name;
char sex;
float score;
//struct date brithday;
};
struct student boy1;
struct student boy2 = { 101,"jame",'M',90.0};
boy1 = boy2;
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", boy2.num, boy2.name, boy2.sex, boy2.score);
#endif
}
执行结果:
Number = 101
Name = &s
Sex = 0
Score = 0.000000
Number =101
Name =&s
Sex=0
Score =0.000000
有错误之前代码:Number = %d\nName = &s\nSex = %c\nScore = %f\n
代码出错更改后:Number = %d\nName = %s\nSex = %c\nScore = %.1f\n
#if 1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void main()
{
#if 0
struct date
{
int month;
int day;
int year;
};
#endif
struct student //定义结构
{
int num;
char* name;
char sex;
float score;
//struct date brithday;
};
struct student boy1;
struct student boy2 = { 101,"jame",'M',90.3};
boy1 = boy2;
printf("Number = %d\nName = %s\nSex = %c\nScore = %.1f\n\n", boy1.num, boy1.name, boy1.sex,boy1.score);
printf("Number = %d\nName = %s\nSex = %c\nScore = %.1f\n", boy2.num, boy2.name, boy2.sex, boy2.score);
#endif
}
结构体数组
1、
一个结构体变量中可以存放一组数据(如一个学生的学号、姓名、成绩等数据)。
2、
如果有10个学生的数据需要参加运算,显然应该用数组,这就是结构体数组。
3、
结构体数组与以前介绍过的数值型数组不同之处在于每个数组元素都是一个结构体类型的数据,它们都分别包括各个成员(分量)项。
#if 1
#define _CRT_SECURE_NO_WARNINGS
#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_s(man[i].name);
printf("input phone:\n");
gets_s(man[i].phone);
}
printf("name\t\t\tphone\n\n");
for (i = 0; i < NUM; i++)
{
printf("%s\t\t\t%s\n", man[i].name, man[i].phone);
}
}
#endif
定义结构体数组
和定义结构体变量的方法相仿,只需说明其为数组即可。建议使用第一种例如:
struct student
{
int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
};
struct student student[3];
或者:
struct student
{
int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
}student[3];
与其他类型的数组一样,对结构体数组可以初始化。例如:
struct student
{
int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
}stu[2]= {
{101,″LiLin″,′M′,18,87.5,″Beijing″}, {102,″Zhang″,′F′,19,99,″Shanghai″}
};
当然,数组的初始化也可以用以下形式:
struct student
{
int num;
…
};
struct student str[]{{…},{…},{…}};
即先声明结构体类型,然后定义数组为该结构体类型,在定义数组时初始化。
例题:对候选人得票的统计程序。设有3个候选人,每次输入一个得票的候选人的名字,要求最后输出各人得票结果。
#if 1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUM 4
struct person
{
char name[20];
int count;
}candidate[NUM] = {
{"小甲鱼", 0},
{"小黑鱼", 0},
{"小白鱼", 0},
{"莫丁丁", 0}
};
char* winner();
void main()
{
int i, j;
char candidate_name[20];
printf("欢迎进入良好公民评选投票系统:() \n\n");
printf("候选人有: 小甲鱼, 小黑鱼, 小白鱼, 莫丁丁(路人甲)\n\n");
for (i = 1; i <= 10; i++)
{
printf("第 %2d 位投票, 请写下支持的候选人名字: ", i);
scanf("%s", candidate_name);
for (j = 0; j < NUM; j++)
{
if (0 == strcmp(candidate_name, candidate[j].name))
{
candidate[j].count++;
}
}
}
printf("\n");
for (i = 0; i < 4; i++)
{
printf("%s 同学得票数为: %d\n", candidate[i].name, candidate[i].count);
}
printf("\n");
printf("本次投票活动的胜利者的: %s", winner());
printf("\n");
system("pause");
}
char* winner()
{
int i = 0, winner = i;
for (i = 1; i < NUM; i++)
{
if (candidate[winner].count < candidate[i].count)
{
winner = i;
}
}
return candidate[winner].name;
}
#endif
1、一个结构体变量的指针就是该结构体变量所占据的内存段的起始地址。
2、可以设一个指针变量,用来指向一个结构体变量,此时该指针变量的值是结构体变量的起始地址。(指针伟大吧,指啥都行~)
3、指针变量也可以用来指向结构体数组中的元素
结构指针变量说明的一般形式为:
struct 结构名 *结构指针变量名
例如,在前面的例题中定义了stu这个结构,如要说明一个指向stu的指针变量pstu,可写为:
struct stu *pstu;
当然也可在定义stu结构时同时说明pstu。与前面讨论的各类指针变量相同,结构指针变量也必须要先赋值后才能使用。
赋值是把结构变量的首地址赋予该指针变量,不能把结构名赋予该指针变量。
如果boy是被说明为stu类型的结构变量,则:
pstu=&boy
是正确的。
pstu=&stu
是错误的。
但是为什么呢?
因为,结构名和结构变量是两个不同的概念,不能混淆。结构名只能表示一个结构形式,编译系统并不对它分配内存空间。
只有当某变量被说明为这种类型的结构时,才对该变量分配存储空间。
因此上面&stu这种写法是错误的,不可能去取一个结构名的首地址。有了结构指针变量,就能更方便地访问结构变量的各个成员。
其访问的一般形式为:
(*结构指针变量).成员名
或为:
结构指针变量->成员名
例如:
(*pstu).num
或者:
pstu->num
#if 1
#include <stdio.h>
struct stu
{
int num;
char *name;
char sex;
float score;
} boy1 = {102, "Fishc", 'M', 78.5};
void main()
{
struct stu *pstu;
pstu = &boy1;
printf("Number = %d\nName = %s\n", boy1.num, boy1.name);
printf("Sex = %c\nScore = %f\n\n", boy1.sex, boy1.score);
printf("Number = %d\nName = %s\n", (*pstu).num, (*pstu).name);
printf("Sex = %c\nScore = %f\n\n", (*pstu).sex, (*pstu).score);
printf("Number = %d\nName = %s\n", pstu->num, pstu->name);
printf("Sex = %c\nScore = %f\n\n", pstu->sex, pstu->score);
}
#endif