建立一个结构体类型。
定义结构体类型变量。
结构体变量的初始化和引用。
例题9-1
方法1
#include
struct student
{
long int number;
char name[20];
char sex;
char addr[20];
}student1 = {10101,"Li Lin",'M',"123 Beijing Road"};
int main(void)
{
printf("number=%ld, name=%s, sex=%c, address=%s", student1.number, student1.name, student1.sex, student1.addr);
return 0;
}
方法2
#include
struct student
{
long int number;
char name[20];
char sex;
char addr[20];
};
int main(void)
{
struct student student1= { 10101,"Li Lin",'M',"123 Beijing Road" };
printf("number=%ld, name=%s, sex=%c, address=%s", student1.number, student1.name, student1.sex, student1.addr);
return 0;
}
#include
struct student
{
long int number;
char name[20];
char sex;
char addr[20];
}student1;
int main(void)
{
student1.number = 10101;
//stduent1.name = "Li Lin";//程序会报错 error C2106: “=”: 左操作数必须为左值
student1.sex = 'M';
//stduent1.addr = "123 Beijing Road";//程序会报错
scanf("%s%s", student1.name, student1.addr);
printf("number=%ld, name=%s, sex=%c, address=%s", student1.number, student1.name, student1.sex, student1.addr);
return 0;
}
#include
struct student
{
long int num;
char name[20];
int grade;
}student1, student2;
int main(void)
{
printf("please type two student number,name,grade:\n");
scanf("%ld%s%d", &student1.num, student1.name,&student1.grade);
scanf("%ld%s%d", &student2.num, student2.name, &student2.grade);
if (student1.grade > student2.grade)
printf("num=%ld,name=%s,grade=%d", student1.num, student1.name, student1.grade);
else
printf("num=%ld,name=%s,grade=%d", student2.num, student2.name, student2.grade);
return 0;
}
#include
struct Person
{
char name[20];
int count;
};
int voter_num = 10;
char name[20];
int string_compare(char* a, char* b);
int main(void)
{
struct Person a[3] = { {"Li",0},{"Zhang",0} ,{"Sun",0} };
for (int i = 0; i < voter_num; i++)
{
scanf("%s", name);
for (int j = 0; j < 3; j++)
if (string_compare(name, a[j].name)==1)
a[j].count++;
}
for (int j = 0; j < 3; j++)
printf("%s=%d\n", a[j].name, a[j].count);
return 0;
}
int string_compare(char *a, char *b)
{
int i = 0;
int flag = 0;
while (*(a + i) != '\0')
{
if (*(a + i) == *(b + i))
flag = 1;
else
{
flag = 0;
break; //只要有一个不满足就退出
}
i++;
};
return flag;
}
#include
struct message
{
long int num;
char name[20];
float grade;
};
int main(void)
{
struct message student[5] = { { 10101,"Zhang",78 },\
{ 10103,"Wang ",98.5 },\
{ 10106,"Li ",86 },\
{ 10108,"Ling ",73.5 },\
{ 10110,"Sun ",100 } };
struct message temp;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4 - i; j++)
if (student[j].grade < student[j + 1].grade)
{
temp = student[j];
student[j] = student[j + 1];
student[j + 1] = temp;
}
}
for (int i = 0; i < 5; i++)
printf("num=%ld,name=%s,grade=%f\n", student[i].num, student[i].name, student[i].grade);
return 0;
}
#include
struct Student
{
long int num;
char name[20];
char sex;
int score;
};
int main(void)
{
struct Student student1 = {10101,"Xiaoming",'M',98};
struct Student *p;
p = &student1;
printf("number=%ld, name=%s, sex=%c, address=%d\n", student1.num, student1.name, student1.sex, student1.score);
printf("number=%ld, name=%s, sex=%c, address=%d\n", (*p).num, (*p).name, (*p).sex, (*p).score);
return 0;
}
#include
struct message
{
long int num;
char name[20];
float grade;
};
int main(void)
{
struct message student[3] = { { 10101,"Zhang",78 },\
{ 10103,"Wang ",98.5 },\
{ 10106,"Li ",86 } };
struct message* p;
p = student;
printf("Method 1:\n");
for(int i = 0;i<3;i++)
printf("num=%ld,name=%s,grade=%f\n", student[i].num, student[i].name, student[i].grade);
printf("Method 2:\n");
for (int i = 0; i < 3; i++)
printf("num=%ld,name=%s,grade=%f\n", (*(p + i)).num, (*(p + i)).name, (*(p + i)).grade);
printf("Method 2:\n");
for (int i = 0; i < 3; i++)
printf("num=%ld,name=%s,grade=%f\n", (p + i)->num, (p + i)->name, (p + i)->grade);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#define N 3
struct message
{
long int num;
char name[20];
float grade[3];
float average;
};
struct message student[N];
struct message* p;
void input(struct message a[]);
struct message max(struct message a[]);
void print(struct message a);
int main(void)
{
p = student;
input(p);
print(max(p));
return 0;
}
void input(struct message a[])
{
printf("please type number,name,grade\n");
for (int i = 0; i < N; i++)
{
scanf("%ld %s %f %f %f", \
& a[i].num, \
a[i].name, \
& a[i].grade[0], \
& a[i].grade[1], \
& a[i].grade[2]);
a[i].average = (a[i].grade[0] + a[i].grade[1] + a[i].grade[2]) / 3.0f;
}
}
struct message max(struct message a[])
{
float max_value = a[0].average;
int m = 0;
for (int i = 0; i < N; i++)
{
if (a[i].average > max_value)
{
max_value = a[i].average;
m = i;
}
}
return a[m];
}
void print(struct message a)
{
printf("number=%ld,name=%s,grade1=%0.2f,grade2=%0.2f,grade3=%0.2f,average=%0.2f", a.num, a.name, a.grade[0], a.grade[1], a.grade[2], a.average);
}
输入的内容如下
10101 Li 78 89 98
10103 Wang 98.5 87 69
10106 Sun 88 76.5 89
#include
struct Student
{
int num;
float socre;
struct Student* next;//指针
};
int main(void)
{
struct Student a, b, c, * head, * p;
a.num = 10101; a.socre = 89.5;
b.num = 10103; b.socre = 90;
c.num = 10107; c.socre = 85;
head = &a;
a.next = &b;
b.next = &c;
c.next = NULL;
p = head;
do
{
printf("%ld %5.1f\n", p->num, p->socre);
p = p->next;
} while (p != NULL);
return 0;
}