15-C语言结构体

时间:2023-03-09 18:10:25
15-C语言结构体

目录:

一、大型软件开发

二、头文件和static

三、结构体

四、联合

五、枚举

回到顶部

一、大型软件开发

将一个代码中的内容,拆分成多个文件,最后的可执行文件只要一个。

操作步骤:

1 原来只有一个文件main.c输入函数 输出函数 声明

2 多人开发 将原文件拆成三个文件,分别为*.h、*c、main.c

3 编译时

1)分别编译不同的源文件,生成相应的目标文件

gcc -c input.c

gcc -c main.c

2)可以将多个目标文件链接生成同一个可执行文件

gcc input.o main.o  =>  a.out

3)在main.c中,引入头文件

4).h文件中的条件编译解决的是重复声明问题

5)先xcode中,Compile Sources解决链接问题的根本

总结:如果使用第三方代码,要做两件事情,导入.h文件,导入目标文件.o(库)

main.c

 #include <stdio.h>
#include "header.h" extern int g_num;
int main(int argc, const char * argv[])
{
int i = ;
i = input();
printf("i:%d\n",i);
printf("g_num:%d\n",getNum());
return ;
}

header.h声明代码:

 #ifndef _______header_h
#define _______header_h int input();
int getNum();
#endif

input.c实现代码:

 #include <stdio.h>
static int g_num = ;//用static修饰只能在本文件中访问
int input()
{
int i = ;
printf("输入一个数:\n");
scanf("%d",&i);
return i;
}
int getNum()
{
return g_num;
}

回到顶部

二、头文件和static

1 以.h结尾的文件就是头文件

2 未使用内存空间的内容都可以放到头文件中,声明变量可能有问题(因为开辟了存储空间),将要使用函数的声明放在头文件中,方便使用函数,如果函数声明变更,则只需要修改头文件的内容,而不需要修改源代码。

3 如果在一个项目*享全局变量,在使用的文件中,使用extern关键字,声明变量,才可以使用,并且可以得到全局变量的值。

4 全局变量前加上static修饰,只能在本文件中使用。

static也可以修饰函数,加了static的变量,就是私有变量,加了static的函数,就是私有函数。

回到顶部

三、结构体

1 在C 中可以使用结构体定义用户自定义的类型,但结构成员类型可以不一样,

2 格式:

struct 结构体名{

成员;

}变量名;

struct student{

int age;

char name[20];

}student1,student2;

3 如果结构体的成员是字符串,是不能通过=号进行赋值,要使用strcpy(student1.name,"zhangsan")

4结构体的定义不占用内存空间,可以把结构体放到头文件中,项目中的其他文件就可以使用该类型。

5 如果是基本数据类型,使用结构类型的变量.成员,可以操作该成员。

6 声明结构体类型的变量,占用空间是所有成员变量所占空间之和。

7 如果使用结构体变量赋值,相当于将结构体中每一个成员的值,都赋值给新的结构体变量的成员。

练习:定义一个学生结构体,学号、姓名、年龄、性别(F、M),创建三个学生,输入信息,并输出。(数组)

 #include <stdio.h>
#include <string.h>
/*
//如果结构体声明到外面,就变成全局变量
//2.定义结构体类型
struct student{
int age;//成员
char name[20];
};
struct student student1 = {18,"zhangsan"},student2 = {19,"lisi"};//struct student是结构体类型,student1是结构体变量
*/ //typedef起别名
//3.定义结构体类型(在以后的编程中通常使用这种)
typedef struct{
int age;//成员
char name[];
}student; /*
练习:定义一个学生结构体,学号、姓名、年龄、性别(F、M),创建三个学生,输入信息,并输出。
*/
//定义学生结构体
typedef struct{
int xuehao;
char name[];
int age;
char sex;
}student; int main(int argc, const char * argv[])
{
/*
//1.定义结构体类型
struct{
int age;//成员
char name[20];
}student1 = {18,"zhangsan"},student2 = {19,"lisi"};//student1声明结构体变量
*/
student student1 = {,"san"};//student是结构体类型,student1是结构体变量名
student student2 = student1;//如果使用结构体变量赋值,相当于将结构体中每一个成员的值,都赋值给新的结构体变量的成员。
student1.age = ;
strcpy(student1.name,"zhangsan");//给name赋值
printf("student1 name:%s,age:%d\n",student1.name,student1.age);
printf("student2 name:%s,age:%d\n",student2.name,student2.age); //作业
student students[];
for (int i = ; i < ; i++) {
printf("输入学生信息:\n");
printf("姓名:\n");
fgets(students[i].name, , stdin);
printf("年龄:\n");
scanf("%d",&students[i].age);
for (int i = ; i < ; i++) {
printf("输入学生信息:\n");
printf("姓名:%s\n",students[i].name);
printf("年龄:%d\n",students[i].age);
}
}
return ;
}

回到顶部

四、联合

1 联合的用法、语法和结构非常相似,但联合中所有成员分配的内存是同一块。(只保存一个成员的信息,联合的空间以最大成员所占的空间为值)

2 联合可以用一块内存对应多种数据类型

3 联合与结构体的区别:

结构可以保存多个成员信息,而联合只能保存一个成员信息且是最后一个。

 #include <stdio.h>
#include <string.h> //定义联合类型
typedef union{
int age;
char name[];
}LianHe;
int main(int argc, const char * argv[])
{
LianHe lh;
lh.age = ;
printf("lh age:%d\n",lh.age);
strcpy(lh.name, "zh");
printf("lh name:%s\n",lh.name);
printf("lh age:%d\n",lh.age);
printf("lh name:%s\n",lh.name);
lh.age = ;
printf("lh age:%d\n",lh.age);
printf("lh name:%s\n",lh.name);
return ;
}

回到顶部

五、枚举

1 用字母来描述一组有规律的数值。

2 定义一个枚举

enum{SBR,SUM,AUT,WIN};

3 枚举默认值从0开始,每个值都是整型常量

4 只能在声明枚举时修改枚举值

5 修改枚举值之后的那个枚举值 = 修改的那个枚举值加1

练习:输入一个数字,得到相应的季节

 #include <stdio.h>
enum{SBR,SUM=,AUT,WIN};
int main(int argc, const char * argv[])
{
printf("%d\n",SBR);
printf("%d\n",SUM);
printf("%d\n",AUT);
printf("%d\n",WIN);
return ;
}