C语言——内联函数、结构体、结构体数组和结构体指针、传递结构体变量和结构体指针

目录

一、内联函数

1.内联函数

2.  #  和  ## 

3.可变参数

 二、结构体

1.结构体声明

2.定义结构体类型变量

3.访问结构体变量

4.初始化结构体变量

5.初始化结构体的指定成员值

三、结构体数组和结构体指针

1.结构体数组

2.初始化结构体数组

3.结构体指针

四、传递结构体变量和结构体指针

1.传递结构体变量

2.传递指向结构体变量的指针

3.动态申请结构体


一、内联函数

1.内联函数

  • C语言引入内联函数来解决程序中函数调用的效率问题。

代码举例:

#include 

inline int square(int x);

inline int square(int x)
{
	return x * x;
}

int main(void)
{
	int i = 1;
	
	while(i <= 100)
	{
		printf("%d的平方是%d\n",i-1,square(i++));
	}
	return 0;
}

运行结果:

C语言——内联函数、结构体、结构体数组和结构体指针、传递结构体变量和结构体指针_第1张图片

  • 内联函数虽然节省了函数调用的时间消耗,但由于每一个函数出现的地方都要进行替换,因此增加了代码编译的时间。另外,并不是所有的函数都能够变成内联函数。
  • 现在的编译器也很聪明,就算不写inline,它也会自动将一些函数优化成内联函数。

2.  #  和  ## 

  • #  和  ##  是两个预处理运算符。
  • 在带参数的宏定义中,# 运算符后面应该跟一个参数,预处理器会把这个参数转换为一个字符串。

代码举例:

#include 

#define STR(s) # s

int main(void)
{
	printf(STR(Hello     %s num = %d),STR(LoveC),520);
	
	return 0;
	
}

运行结果:

C语言——内联函数、结构体、结构体数组和结构体指针、传递结构体变量和结构体指针_第2张图片

  • ## 运算符被称为记号连接运算符,比如我们可以使用 ## 运算符连接两个参数。

代码举例:

#include 

#define TOGETHER(x,y) x ## y

int main(void)
{
	printf("%d\n",TOGETHER(2,50));
	
	return 0;
	
}

运行结果:

C语言——内联函数、结构体、结构体数组和结构体指针、传递结构体变量和结构体指针_第3张图片

3.可变参数

  • 之前我们学习了如何让函数支持可变参数,带参数的宏定义也是使用可变参数的:

#define SHOWLIST(…) printf(#__VA_ARGS__)

  • 其中,…表示使用可变参数,__VA_ARGS__在预处理中被实际的参数集所替换。

代码举例:

#include 

#define SHOWLIST(...) printf(# __VA_ARGS__)

int main(void)
{
	SHOWLIST(LoveC,520,3.14\n);
	
	return 0;
	
}

运行结果:

C语言——内联函数、结构体、结构体数组和结构体指针、传递结构体变量和结构体指针_第4张图片

 二、结构体

1.结构体声明

结构体声明是描述结构体组合的主要方法。

C语言——内联函数、结构体、结构体数组和结构体指针、传递结构体变量和结构体指针_第5张图片

2.定义结构体类型变量

struct 结构体名称 结构体变量名

代码举例:

#include 

struct Book
{
	char title[128];
	char author[40];
	float price;
	unsigned int date;
	char publisher[40];
}book;//全局变量 

int main(void)
{
//	struct Book book;//局部变量 

	return 0;
	
}

3.访问结构体变量

  • 要访问结构体成员,需要引入一个新的运算符一点号(.)运算符。比如book.title就是引用book结构体的title成员,它是一个字符数组;而book.price则是引用book结构体的price成员,它是一个浮点型的变量。

代码举例:

#include 

struct Book
{
	char title[128];
	char author[40];
	float price;
	unsigned int date;
	char publisher[40];
}book;

int main(void)
{
	printf("请输入书名:");
	scanf("%s",book.title);
	printf("请输入作者:");
	scanf("%s",book.author);
	printf("请输入售价:");
	scanf("%f",&book.price);
	printf("请输入出版日期:");
	scanf("%d",&book.date);
	printf("请输入出版社:");
	scanf("%s",book.publisher);
	
	printf("\n=====数据录入完毕=====\n");
	
	printf("\n书名:%s\n",book.title);
	printf("作者:%s\n",book.author);
	printf("售价:%2f\n",book.price);
	printf("出版日期:%d\n",book.date);
	printf("出版社:%s\n",book.publisher);
	
	return 0;
	
}

运行结果:

C语言——内联函数、结构体、结构体数组和结构体指针、传递结构体变量和结构体指针_第6张图片

4.初始化结构体变量

  • 初始化一个变量和数组:
int a = 520;
int array[5] = {1, 2, 3, 4, 5};
  • 初始化一个结构体变量:
struct Book book = {
    "《带你学C带你飞》",
    "小甲鱼",
    48.8,
    20220817,
    "清华大学出版社"
};

5.初始化结构体的指定成员值

  • 其语法和数组指定初始化元素类似,不过结构体指定初始化成员使用点号(.)运算符和成员名。
  • 比如可以让程序只初始化Book的price成员:
struct Book book = {.price = 48.8};
  • 还可以不按结构体声明的成员顺序进行初始化:
struct Book book = {
    .publisher = "清华大学出版社",
    .price = 48.8,
    .date = 20220817
};

三、结构体数组和结构体指针

1.结构体数组

  • 第一种方法是在声明结构体的时候进行定义:
struct 结构体名称
{
    结构体成员;
}数组名[长度];
  • 第二种方法是先声明一个结构体类型(比如上面Book),再用此类型定义一个结构体数组:
struct 结构体名称
{
    结构体成员;
};
struct 结构体名称 数组名[长度];

2.初始化结构体数组

struct Book book[3]={
    {"《零基础入门学习Python》","小甲鱼",49.5,{2016,11,11},"清华大学出版社"},
    {"《零基础入门学习Scratch》","不二如是",49.9,{2017,10,1},"清华大学出版社“},
    {"《带你学C带你飞》","小甲鱼",49.9,{2017,11,11},"清华大学出版社"}
};

3.结构体指针

struct Book * pt;
pt = &book;

通过结构体指针访问结构体成员有两种方法:

  • (*结构体指针).成员名
  • 结构体指针->成员名

四、传递结构体变量和结构体指针

1.传递结构体变量

两个结构体变量可以直接赋值

代码举例1:

#include 

int main(void)
{
	struct Test
	{
		int x;
		int y;
	}t1,t2;
	
	t1.x = 3;
	t2.y = 4;
	
	t2 = t1;
	
	printf("t2.x = %d,t2.y = %d\n",t2.x,t2.y);
	
}

运行结果:

C语言——内联函数、结构体、结构体数组和结构体指针、传递结构体变量和结构体指针_第7张图片

 代码举例2:

#include 

struct Date
{
	int year;
	int month;
	int day;
};

struct Book
{
	char title[128];
	char author[40];
	float price;
	struct Date date;
	char publisher[40];
};

struct Book getInput(struct Book book);
void printBook(struct Book book);

struct Book getInput(struct Book book)
{
	printf("请输入书名:");
	scanf("%s",book.title);
	printf("请输入作者:");
	scanf("%s",book.author);
	printf("请输入售价:");
	scanf("%f",&book.price);
	printf("请输入出版日期:");
	scanf("%d-%d-%d",&book.date.year,&book.date.month,&book.date.day);
	printf("请输入出版社:");
	scanf("%s",book.publisher);
	
	return book;
}

void printBook(struct Book book)
{
	printf("书名:%s\n",book.title);
	printf("作者:%s\n",book.author);
	printf("售价:%.2f\n",book.price);
	printf("出版日期:%d-%d-%d\n",book.date.year,book.date.month,book.date.day);
	printf("出版社:%s\n",book.publisher);
}



int main(void)
{
	struct Book b1,b2;
	
	printf("请录入第一本书的信息 ……\n");
	b1 = getInput(b1);
	putchar('\n');
	printf("请录入第二本书的信息 ……\n");
	b2 = getInput(b2);
	
	printf("\n\n录入完毕,现在开始打印验证……\n\n");
	
	printf("打印第一本书的信息……\n");
	printBook(b1);
	putchar('\n');
	printf("打印第二本书的信息……\n");
	printBook(b2);
	
	return 0;
	
}

运行结果:

C语言——内联函数、结构体、结构体数组和结构体指针、传递结构体变量和结构体指针_第8张图片

2.传递指向结构体变量的指针

上述代码改为:

#include 

struct Date
{
	int year;
	int month;
	int day;
};

struct Book
{
	char title[128];
	char author[40];
	float price;
	struct Date date;
	char publisher[40];
};

void getInput(struct Book *book);
void printBook(struct Book *book);

void getInput(struct Book *book)
{
	printf("请输入书名:");
	scanf("%s",book->title);
	printf("请输入作者:");
	scanf("%s",book->author);
	printf("请输入售价:");
	scanf("%f",&book->price);
	printf("请输入出版日期:");
	scanf("%d-%d-%d",&book->date.year,&book->date.month,&book->date.day);
	printf("请输入出版社:");
	scanf("%s",book->publisher);
	
	return book;
}

void printBook(struct Book *book)
{
	printf("书名:%s\n",book->title);
	printf("作者:%s\n",book->author);
	printf("售价:%.2f\n",book->price);
	printf("出版日期:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
	printf("出版社:%s\n",book->publisher);
}



int main(void)
{
	struct Book b1,b2;
	
	printf("请录入第一本书的信息 ……\n");
	getInput(&b1);
	putchar('\n');
	printf("请录入第二本书的信息 ……\n");
	getInput(&b2);
	
	printf("\n\n录入完毕,现在开始打印验证……\n\n");
	
	printf("打印第一本书的信息……\n");
	printBook(&b1);
	putchar('\n');
	printf("打印第二本书的信息……\n");
	printBook(&b2);
	
	return 0;
	
}

3.动态申请结构体

使用malloc函数为结构体分配存储空间

上述代码改为:

#include 
#include 


struct Date
{
	int year;
	int month;
	int day;
};

struct Book
{
	char title[128];
	char author[40];
	float price;
	struct Date date;
	char publisher[40];
};

void getInput(struct Book *book);
void printBook(struct Book *book);

void getInput(struct Book *book)
{
	printf("请输入书名:");
	scanf("%s",book->title);
	printf("请输入作者:");
	scanf("%s",book->author);
	printf("请输入售价:");
	scanf("%f",&book->price);
	printf("请输入出版日期:");
	scanf("%d-%d-%d",&book->date.year,&book->date.month,&book->date.day);
	printf("请输入出版社:");
	scanf("%s",book->publisher);
	
	return book;
}

void printBook(struct Book *book)
{
	printf("书名:%s\n",book->title);
	printf("作者:%s\n",book->author);
	printf("售价:%.2f\n",book->price);
	printf("出版日期:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
	printf("出版社:%s\n",book->publisher);
}



int main(void)
{
	struct Book *b1,*b2;
	
	b1 = (struct Book *)malloc(sizeof(struct Book));
	b2 = (struct Book *)malloc(sizeof(struct Book));
	
	if (b1 == NULL || b2 == NULL)
	{
		printf("内存分配失败!\n");
		exit(1);
	}
	
	printf("请录入第一本书的信息 ……\n");
	getInput(b1);
	putchar('\n');
	printf("请录入第二本书的信息 ……\n");
	getInput(b2);
	
	printf("\n\n录入完毕,现在开始打印验证……\n\n");
	
	printf("打印第一本书的信息……\n");
	printBook(b1);
	putchar('\n');
	printf("打印第二本书的信息……\n");
	printBook(b2);
	
	free(b1);
	free(b2);
	
	return 0;
	
}

你可能感兴趣的:(c语言,c++,算法)