[toc]
条件编译和结构体
一、条件编译
编译器根据条件真假决定是否编译相关代码
常见条件编译:
根据宏是否定义:
c#ifdef <macro> 如果定义宏要编译的代码; #else 宏没有被定义时要编译的代码; #endif根据宏值:
c#if <macro> 宏值为真时要编译的代码; #else 宏值不为真时要编译的代码 #endif
二、结构体
- 在实际的处理对象中, 许多信息是由多个不同类型的数据组合在一起进行描述, 不同类型数据联系成为一个整体. 这个整体使用一种新的类型来描述--这个类型称为结构体(structure)
- 结构体为处理复杂的数据结构提供了有效的手段
2.1 结构体语法
c
struct 结构体名
{
数据类型 成员名1;
数据类型 成员名2;
..
数据类型 成员名n;
};- 大括号内的内容称为成员列表
- 成员名命名规则与标识符一致
- 数据类型可以是基本类型, 数组类型, 或者是一个结构体类型
- 成员之间用分号(
;)隔开, 整个结构体用分号(;)作为结束符
示例
c
struct worker
{
long number;
char name[20];
char sex;
int age;
float salary;
char address[100];
};
int age = 10;worker中的age为成员变量, 与结构体外的age变量不冲突.
==特点==
- 用户自行构造的
- 由若干不同的基本数据类型的数据组成
- 属于C语言的一种数据类型, 与整型、实型相当. 定义时不分配空间, 用他定义变量时才分配空间.
2.2 定义结构体变量
先定义结构体类型再定义变量名
cstruct 结构体名 { 成员列表; }; struct 结构体名 变量名;struct 结构体名: 代表类型, 不能分开写- 为了使用方便, 通常可以进行宏定义
#define 自定义类型名 struct 结构体名
在定义结构体类型时定义结构体变量
cstruct 结构体名 { 成员列表; }变量名;直接定义结构体变量
cstruct { 成员列表; }变量名;- 常用于内嵌结构体成员中.
2.3 结构体变量的使用
结构体变量是不同数据类型的若干个数据的集合, 在程序中使用结构体变量时, 不能把结构体变量作为一个整体参加数据处理. 而是结构体变量中的成员项数据参与数据处理
==语法==:
结构体变量.成员名: 成员不是结构体
结构体变量.一级成员名.二级成员名: 成员是结构体
==同一类型的结构体可以相互赋值==
示例代码
c
#include <stdio.h>
#include <string.h>
struct student
{
int num;
float score;
char name[20];
};
int main()
{
struct student std;
std.num = 1001;
strcpy(std.name, "小小");
std.score = 90;
printf("std.name = %s, std.num = %d, std.score = %f\n", std.name, std.num, std.score);
return 0;
}2.4 结构体变量初始化
- c
struct 结构体名 结构体变量名 = {初始数据表} - c
struct 结构体名 { 成员列表; }变量名 = {初始数据表}; - c
struct { 成员列表; }变量名 = {初始数据表};
示例程序
c
#include <stdio.h>
#include <string.h>
struct birthday
{
int year;
int month;
int day;
};
struct student
{
int num;
float score;
char name[20];
struct birthday age;
}s1 = {1002, 99, "小明", {1996, 12, 24}};
int main()
{
struct student std;
std.num = 1001;
strcpy(std.name, "小小");
std.score = 90;
std.age.year = 1998;
std.age.month = 6;
std.age.day = 23;
printf("std.name = %s, std.num = %d, std.score = %f std.age=%d-%d-%d\n", std.name, std.num, std.score, std.age.year, std.age.month, std.age.day);
printf("s1.name = %s, s1.num = %d, s1.score = %f s1.age=%d-%d-%d\n", s1.name, s1.num, s1.score, s1.age.year, s1.age.month, s1.age.day);
return 0;
}2.5 结构体数组
- 具有相同数据类型的结构体变量构成一个数组, 称为结构体数组.
- 结构体数组的每一个数组元素都是结构体类型数据. 他们分别包括各个成员项.
==定义结构体数组语法==
先定义结构体, 在定义结构体数组
cstruct 结构体名 { 成员列表; }; struct 结构体名 结构体数组名[数组大小];定义结构体类型时定义结构体数组
cstruct 结构体名 { 成员列表; }结构体数组名[数组大小];直接定义结构体数组
cstruct { 成员列表; }结构体数组名[数组大小];
2.6 结构体数组初始化
- c
truct 结构体名 { 成员列表; }; struct 结构体名 结构体数组名[数组大小]={初始数据列表}; - c
struct 结构体名 { 成员列表; }结构体数组名[数组大小]={初始数据列表}; - c
struct { 成员列表; }结构体数组名[数组大小]={初始数据列表};
2.7 结构体数组的使用
结构体数组名[i].成员名(结构数组首地址+i)->成员名- 结构体数组的
首地址是结构体数组名
- 结构体数组的
==示例程序==
c
#include <stdio.h>
struct student
{
int no;
char name[20];
float score;
}; // 定义一个结构体
int main()
{
struct student std[]={{1,"std1", 80}, {2, "std2", 89}, {3, "std3", 91}}; //定义一个结构体数组并初始化
for (int i = 0; i < 3; i++)
{
printf("std%d.no = %d, std%d.name=%s, std%d.score = %.3f\n", i, (std+i)->no, i, (std+i)->name, i, (std+i)->score); // 通过结构体数组首地址访问各个元素的成员
printf("std%d.no = %d, std%d.name=%s, std%d.score = %.3f\n", i, std[i].no, i, std[i].name, i, std[i].score); // 通过下标访问个元素的成员
}
return 0;
}2.8 结构体与指针
结构体指针
指向结构体的指针变量.值是结构体变量的首地址.
语法
cstruct 结构体名 * 结构体指针名 [= 结构体变量的首地址];- 结构体名必须是定义后的结构体.
- 指向的目标的数据类型是
struct 结构体名.
初始化
cstruct 结构体名 * 结构体指针名; 结构体指针名 = &结构体变量名;cstruct 结构体名 * 结构体指针名 [= 结构体变量的首地址];通过结构体指针访问成员
c结构体指针变量->成员名; (*结构体指针指针变量).成员名;
结构体数组指针
指向结构体数组的指针, 值是结构体数组的首地址.
语法
cstruct 结构体名 * 结构体数组指针名 [= 结构体数组的首地址];- 结构体名必须是定义后的结构体.
- 指向的目标的数据类型是
struct 结构体名类型的数组
示例程序
c#include <stdio.h> struct student { int no; char name[20]; float score; }; // 定义一个结构体 int main() { struct student s={4,"s1", 80}; struct student *ps; // 定义一个结构体指针变量指向结构体变量 ps = &s; printf("s.no = %d - s.name = %s - s.score = %f\n", (*ps).no, (*ps).name, (*ps).score); printf("s.no = %d - s.name = %s - s.score = %f\n", ps->no, ps->name, ps->score); // 结构体数组指针 struct student std[] = {{1, "std1", 80},{2, "std2", 90},{3, "std3", 87}}; // 定义一个结构体数组, 并初始化 struct student *pstd = std; // 定义一个结构体指针变量, 指向结构体数组 int i, n=sizeof(std)/sizeof(std[0]); for(i = 0; i < n; i++) { printf("指针访问结构体数组(指针名->成员名): std.no=%d std.name = %s std.score = %f\n", pstd->no, pstd->name, pstd->score); printf("指针访问结构体数组(*(指针名).成员名): std.no=%d std.name = %s std.score = %f\n", (*pstd).no, (*pstd).name, (*pstd).score); pstd++; } return 0; }