C primer plus自用知識點整理(第十四章)結構和其他數據形式

書籍整理內容:
最近在看C primer plus(加深鞏固自己的C語言技巧,爲以後學習C++打個基礎)。
裏面知識針對自己以後要查的點整理出來。
使用工具:visual studio 2013
第二、三章內容:概述、變量、基本數據類型等:https://blog.csdn.net/answerMack/article/details/103766020
第四章內容:字符串和格式化輸入輸出:https://blog.csdn.net/answerMack/article/details/103805900
第五章內容:運算符、表達式和語句:https://blog.csdn.net/answerMack/article/details/103855794
第六章內容:循壞、賦值運算符等:https://blog.csdn.net/answerMack/article/details/103870182
第七章內容:if、if else、?:、switch、goto、continue、邏輯運算符優先級https://blog.csdn.net/answerMack/article/details/103891048
第八章內容:字符輸入輸出函數、輸入驗證(混合輸入)https://blog.csdn.net/answerMack/article/details/103953376
第九章內容:函數和指針:https://blog.csdn.net/answerMack/article/details/103978471
第十章:數組和指針:https://blog.csdn.net/answerMack/article/details/104114028
第十一章:字符串和字符串函數:https://blog.csdn.net/answerMack/article/details/105222269
第十二章:存儲類別、鏈接和內存管理:https://blog.csdn.net/answerMack/article/details/105338533
第十三章未更新

在這裏插入圖片描述

結構體

//* book.c -- one-book inventory */
#include <stdio.h>
#include <string.h>
char * s_gets(char * st, int n);
#define MAXTITL  41      /* maximum length of title + 1         */
#define MAXAUTL  31      /* maximum length of author's name + 1 */

struct book {            /* structure template: tag is book     */
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
};                       /* end of structure template           */

int main(void)
{
	struct book library; /* declare library as a book variable  */

	printf("Please enter the book title.\n");
	s_gets(library.title, MAXTITL); /* access to the title portion         */
	printf("Now enter the author.\n");
	s_gets(library.author, MAXAUTL);
	printf("Now enter the value.\n");
	scanf_s("%f", &library.value);
	printf("%s by %s: $%.2f\n", library.title,
		library.author, library.value);
	printf("%s: \"%s\" ($%.2f)\n", library.author,
		library.title, library.value);
	printf("Done.\n");

	return 0;
}

char * s_gets(char * st, int n)
{
	char * ret_val;
	char * find;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n');   // look for newline
		if (find)                  // if the address is not NULL,
			*find = '\0';          // place a null character there
		else
			while (getchar() != '\n')
				continue;          // dispose of rest of line
	}
	return ret_val;
}

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

結構體的初始化:

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

訪問結構成員

在這裏插入圖片描述

結構數組

/* manybook.c -- multiple book inventory */
#include <stdio.h>
#include <string.h>

char * s_gets(char * st, int n);
#define MAXTITL   40
#define MAXAUTL   40
#define MAXBKS   100              /* maximum number of books  */

struct book {                     /* set up book template     */
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
};

int main(void)
{
	struct book library[MAXBKS]; /* array of book structures */
	int count = 0;
	int index;

	printf("Please enter the book title.\n");
	printf("Press [enter] at the start of a line to stop.\n");
	while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL
		&& library[count].title[0] != '\0')
	{
		printf("Now enter the author.\n");
		s_gets(library[count].author, MAXAUTL);
		printf("Now enter the value.\n");
		scanf_s("%f", &library[count++].value);
		while (getchar() != '\n')
			continue;          /* clear input line         */
		if (count < MAXBKS)
			printf("Enter the next title.\n");
	}

	if (count > 0)
	{
		printf("Here is the list of your books:\n");
		for (index = 0; index < count; index++)
			printf("%s by %s: $%.2f\n", library[index].title,
				library[index].author, library[index].value);
	}
	else
		printf("No books? Too bad.\n");

	return 0;
}

char * s_gets(char * st, int n)
{
	char * ret_val;
	char * find;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n');   // look for newline
		if (find)                  // if the address is not NULL,
			*find = '\0';          // place a null character there
		else
			while (getchar() != '\n')
				continue;          // dispose of rest of line
	}
	return ret_val;
}

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

程序討論

在這裏插入圖片描述
在這裏插入圖片描述

嵌套結構

#include <stdio.h>
#define LEN 20
const char * msgs[5] =
{
	"    Thank you for the wonderful evening, ",
	"You certainly prove that a ",
	"is a special kind of guy. We must get together",
	"over a delicious ",
	" and have a few laughs"
};

struct names {                     // first structure
	char first[LEN];
	char last[LEN];
};

struct guy {                       // second structure
	struct names handle;           // nested structure
	char favfood[LEN];
	char job[LEN];
	float income;
};

int main(void)
{
	struct guy fellow = {   // initialize a variable
		{ "Ewen", "Villard" },
		"grilled salmon",
		"personality coach",
		68112.00
	};

	printf("Dear %s, \n\n", fellow.handle.first);
	printf("%s%s.\n", msgs[0], fellow.handle.first);
	printf("%s%s\n", msgs[1], fellow.job);
	printf("%s\n", msgs[2]);
	printf("%s%s%s", msgs[3], fellow.favfood, msgs[4]);
	if (fellow.income > 150000.0)
		puts("!!");
	else if (fellow.income > 75000.0)
		puts("!");
	else
		puts(".");
	printf("\n%40s%s\n", " ", "See you soon,");
	printf("%40s%s\n", " ", "Shalala");

	return 0;
}

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

指向結構的指針

在這裏插入圖片描述

#include <stdio.h>
#define LEN 20

struct names {
	char first[LEN];
	char last[LEN];
};

struct guy {
	struct names handle;
	char favfood[LEN];
	char job[LEN];
	float income;
};

int main(void)
{
	struct guy fellow[2] = {
		{ { "Ewen", "Villard" },
		"grilled salmon",
		"personality coach",
		68112.00
		},
		{ { "Rodney", "Swillbelly" },
		"tripe",
		"tabloid editor",
		432400.00
		}
	};
	struct guy * him;    /* here is a pointer to a structure */

	printf("address #1: %p #2: %p\n", &fellow[0], &fellow[1]);
	him = &fellow[0];    /* tell the pointer where to point  */
	printf("pointer #1: %p #2: %p\n", him, him + 1);
	printf("him->income is $%.2f: (*him).income is $%.2f\n",
		him->income, (*him).income);
	him++;               /* point to the next structure      */
	printf("him->favfood is %s:  him->handle.last is %s\n",
		him->favfood, him->handle.last);

	return 0;
}

在這裏插入圖片描述

指針聲明

在這裏插入圖片描述
在這裏插入圖片描述

指針訪問成員

第一種:- >
在這裏插入圖片描述
第二種:(*him).income
在這裏插入圖片描述
之前寫過的程序的例子鏈接:PID
https://blog.csdn.net/answerMack/article/details/83900027

向函數傳遞結構的信息

函數與結構交互

傳遞結構成員

#include <stdio.h>
#define FUNDLEN 50

struct funds {
	char   bank[FUNDLEN];
	double bankfund;
	char   save[FUNDLEN];
	double savefund;
};

double sum(double, double);

int main(void)
{
	struct funds stan = {
		"Garlic-Melon Bank",
		4032.27,
		"Lucky's Savings and Loan",
		8543.94
	};

	printf("Stan has a total of $%.2f.\n",
		sum(stan.bankfund, stan.savefund));
	return 0;
}

/* adds two double numbers */
double sum(double x, double y)
{
	return(x + y);
}

傳遞結構地址

主函數:

sum(&stan)

求和函數

double sum(const struct funds * money)
{
	return(money->bankfund + money->savefund);
}

傳遞結構

主函數:

sum(stan)

求和函數

double sum(struct funds money)
{
	return(money.bankfund + money.savefund);
}

其他結構特性

在這裏插入圖片描述
使用指針:傳遞地址,且函數爲void形式

#include <stdio.h>
#include <string.h>

#define NLEN 30
struct namect {
	char fname[NLEN];
	char lname[NLEN];
	int letters;
};

void getinfo(struct namect *);
void makeinfo(struct namect *);
void showinfo(const struct namect *);
char * s_gets(char * st, int n);

int main(void)
{
	struct namect person;

	getinfo(&person);
	makeinfo(&person);
	showinfo(&person);
	return 0;
}

void getinfo(struct namect * pst)
{
	printf("Please enter your first name.\n");
	s_gets(pst->fname, NLEN);
	printf("Please enter your last name.\n");
	s_gets(pst->lname, NLEN);
}

void makeinfo(struct namect * pst)
{
	pst->letters = strlen(pst->fname) +
		strlen(pst->lname);
}

void showinfo(const struct namect * pst)
{
	printf("%s %s, your name contains %d letters.\n",
		pst->fname, pst->lname, pst->letters);
}

char * s_gets(char * st, int n)
{
	char * ret_val;
	char * find;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n');   // look for newline
		if (find)                  // if the address is not NULL,
			*find = '\0';          // place a null character there
		else
			while (getchar() != '\n')
				continue;          // dispose of rest of line
	}
	return ret_val;
}

在這裏插入圖片描述
在這裏插入圖片描述
使用結構:傳遞結構

#include <stdio.h>
#include <string.h>

#define NLEN 30
struct namect {
	char fname[NLEN];
	char lname[NLEN];
	int letters;
};

struct namect getinfo(void);
struct namect makeinfo(struct namect);
void showinfo(struct namect);
char * s_gets(char * st, int n);

int main(void)
{
	struct namect person;

	person = getinfo();
	person = makeinfo(person);
	showinfo(person);

	return 0;
}

struct namect getinfo(void)
{
	struct namect temp;
	printf("Please enter your first name.\n");
	s_gets(temp.fname, NLEN);
	printf("Please enter your last name.\n");
	s_gets(temp.lname, NLEN);

	return temp;
}

struct namect makeinfo(struct namect info)
{
	info.letters = strlen(info.fname) + strlen(info.lname);

	return info;
}

void showinfo(struct namect info)
{
	printf("%s %s, your name contains %d letters.\n",
		info.fname, info.lname, info.letters);
}

char * s_gets(char * st, int n)
{
	char * ret_val;
	char * find;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n');   // look for newline
		if (find)                  // if the address is not NULL,
			*find = '\0';          // place a null character there
		else
			while (getchar() != '\n')
				continue;          // dispose of rest of line
	}
	return ret_val;
}

函數定義中包含返回值,可以返回該結構。
函數聲明:struct namect makeinfo(struct namect);
在這裏插入圖片描述

結構和結構指針選擇

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

結構中的字符數組和字符指針

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

結構、指針和malloc()

malloc動態分配內存(詳情見12章)與free函數相結合使用
在這裏插入圖片描述

#include <stdio.h>
#include <string.h>   // for strcpy(), strlen()
#include <stdlib.h>   // for malloc(), free()
#define SLEN 81
struct namect {
	char * fname;  // using pointers
	char * lname;
	int letters;
};

void getinfo(struct namect *);        // allocates memory
void makeinfo(struct namect *);
void showinfo(const struct namect *);
void cleanup(struct namect *);        // free memory when done
char * s_gets(char * st, int n);

int main(void)
{
	struct namect person;

	getinfo(&person);
	makeinfo(&person);
	showinfo(&person);
	cleanup(&person);

	return 0;
}

void getinfo(struct namect * pst)
{
	char temp[SLEN];
	printf("Please enter your first name.\n");
	s_gets(temp, SLEN);
	// allocate memory to hold name
	pst->fname = (char *)malloc(strlen(temp) + 1);
	// copy name to allocated memory
	strcpy_s(pst->fname, strlen(temp)+1, temp);
	printf("Please enter your last name.\n");
	s_gets(temp, SLEN);
	pst->lname = (char *)malloc(strlen(temp) + 1);
	strcpy_s(pst->lname, strlen(temp)+1,temp);
}

void makeinfo(struct namect * pst)
{
	pst->letters = strlen(pst->fname) +
		strlen(pst->lname);
}

void showinfo(const struct namect * pst)
{
	printf("%s %s, your name contains %d letters.\n",
		pst->fname, pst->lname, pst->letters);
}

void cleanup(struct namect * pst)
{
	free(pst->fname);
	free(pst->lname);
}

char * s_gets(char * st, int n)
{
	char * ret_val;
	char * find;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n');   // look for newline
		if (find)                  // if the address is not NULL,
			*find = '\0';          // place a null character there
		else
			while (getchar() != '\n')
				continue;          // dispose of rest of line
	}
	return ret_val;
}

在這裏插入圖片描述

複合字面量和結構(c99)

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
複合字面量可以不需要變量名!!

伸縮型數組成員(c99)

在這裏插入圖片描述
在這裏插入圖片描述

書本例程程序有錯誤(14.12)
#include <stdio.h>
#include <stdlib.h>

struct flex
{
	size_t count;
	double average;
	double scores[];   // flexible array member
};

void showFlex(const struct flex * p);

int main(void)
{
	struct flex * pf1, *pf2;
	int n = 5;
	int i;
	int tot = 0;

	// allocate space for structure plus array
	pf1 = (struct flex *)malloc(sizeof(struct flex) + n * sizeof(double));
	pf1->count = n;
	for (i = 0; i < n; i++)
	{
		pf1->scores[i] = 20.0 - i;
		tot += pf1->scores[i];
	}
	pf1->average = tot / n;
	showFlex(pf1);

	n = 9;
	tot = 0;
	pf2 = (struct flex *)malloc(sizeof(struct flex) + n * sizeof(double));
	pf2->count = n;
	for (i = 0; i < n; i++)
	{
		pf2->scores[i] = 20.0 - i / 2.0;
		tot += pf2->scores[i];
	}
	pf2->average = tot / n;
	showFlex(pf2);
	free(pf1);
	free(pf2);

	return 0;
}

void showFlex(const struct flex * p)
{
	int i;
	printf("Scores : ");
	for (i = 0; i < p->count; i++)
		printf("%g ", p->scores[i]);
	printf("\nAverage: %g\n", p->average);
}


錯誤語句:
pf1 = (struct flex *)malloc(sizeof(struct flex) + n * sizeof(double));
pf2 = (struct flex *)malloc(sizeof(struct flex) + n * sizeof(double));
malloc應堅持使用強制類型轉換!!!


結果:
在這裏插入圖片描述
在這裏插入圖片描述

匿名結構(c11)

在這裏插入圖片描述
在這裏插入圖片描述

使用結構數組的函數

在這裏插入圖片描述

#include <stdio.h>
#define FUNDLEN 50
#define N 2

struct funds {
	char   bank[FUNDLEN];
	double bankfund;
	char   save[FUNDLEN];
	double savefund;
};

double sum(const struct funds money[], int n);

int main(void)
{
	struct funds jones[N] = {
		{
			"Garlic-Melon Bank",
			4032.27,
		"Lucky's Savings and Loan",
		8543.94

		},
		{
			"Honest Jack's Bank",
			3620.88,
		"Party Time Savings",
		3802.91
		}
	};

	printf("The Joneses have a total of $%.2f.\n",
		sum(jones, N));

	return 0;
}

double sum(const struct funds money[], int n)
{
	double total;
	int i;

	for (i = 0, total = 0; i < n; i++)
		total += money[i].bankfund + money[i].savefund;

	return(total);
}

在這裏插入圖片描述

把結構內容保存到文件中(待理解+13章內容)

在這裏插入圖片描述
fprintf(見11章)
在這裏插入圖片描述
在這裏插入圖片描述


(文件操作見13章,本人未寫)
在這裏插入圖片描述
fopen_s(&pbooks,“book.dat”, “a+b”)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTITL  40
#define MAXAUTL  40
#define MAXBKS   10             /* maximum number of books */

char * s_gets(char * st, int n);

struct book {                   /* set up book template    */
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
};

int main(void)
{
	struct book library[MAXBKS]; /* array of structures     */
	int count = 0;
	int index, filecount;
	FILE * pbooks;
	int size = sizeof(struct book);

	if ((fopen_s(&pbooks,"book.dat", "a+b")) == NULL)
	{
		fputs("Can't open book.dat file\n", stderr);
		exit(1);
	}

	rewind(pbooks);            /* go to start of file     */
	while (count < MAXBKS &&  fread(&library[count], size,
		1, pbooks) == 1)
	{
		if (count == 0)
			puts("Current contents of book.dat:");
		printf("%s by %s: $%.2f\n", library[count].title,
			library[count].author, library[count].value);
		count++;
	}
	filecount = count;
	if (count == MAXBKS)
	{
		fputs("The book.dat file is full.", stderr);
		exit(2);
	}

	puts("Please add new book titles.");
	puts("Press [enter] at the start of a line to stop.");
	while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL
		&& library[count].title[0] != '\0')
	{
		puts("Now enter the author.");
		s_gets(library[count].author, MAXAUTL);
		puts("Now enter the value.");
		scanf_s("%f", &library[count++].value);
		while (getchar() != '\n')
			continue;                /* clear input line  */
		if (count < MAXBKS)
			puts("Enter the next title.");
	}

	if (count > 0)
	{
		puts("Here is the list of your books:");
		for (index = 0; index < count; index++)
			printf("%s by %s: $%.2f\n", library[index].title,
				library[index].author, library[index].value);
		fwrite(&library[filecount], size, count - filecount,
			pbooks);
	}
	else
		puts("No books? Too bad.\n");

	puts("Bye.\n");
	fclose(pbooks);

	return 0;
}

char * s_gets(char * st, int n)
{
	char * ret_val;
	char * find;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n');   // look for newline
		if (find)                  // if the address is not NULL,
			*find = '\0';          // place a null character there
		else
			while (getchar() != '\n')
				continue;          // dispose of rest of line
	}
	return ret_val;
}

在這裏插入圖片描述


把//exit(1);註釋掉了的下面結果:
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

鏈式結構(簡介,內容少)

在這裏插入圖片描述

聯合簡介(union)

在這裏插入圖片描述
在這裏插入圖片描述

用法

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

匿名聯合(c11)

在這裏插入圖片描述

結構和union運算符

在這裏插入圖片描述

枚舉類型enum

enum常量爲int類型。
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

用法

在這裏插入圖片描述

#include <stdio.h>
#include <string.h>    // for strcmp(), strchr()
#include <stdbool.h>   // C99 feature
char * s_gets(char * st, int n);

enum spectrum { red, orange, yellow, green, blue, violet };
const char * colors[] = { "red", "orange", "yellow",
"green", "blue", "violet" };
#define LEN 30

int main(void)
{
	char choice[LEN];
	int  color;
	bool color_is_found = false;

	puts("Enter a color (empty line to quit):");
	while (s_gets(choice, LEN) != NULL && choice[0] != '\0')
	{
		for (color = red; color <= violet; color++)
		{
			if (strcmp(choice, colors[color]) == 0)
			{
				color_is_found = true;
				break;
			}
		}
		if (color_is_found)
			switch (color)
			{
			case red: puts("Roses are red.");
				break;
			case orange: puts("Poppies are orange.");
				break;
			case yellow: puts("Sunflowers are yellow.");
				break;
			case green: puts("Grass is green.");
				break;
			case blue: puts("Bluebells are blue.");
				break;
			case violet: puts("Violets are violet.");
				break;
			}
		else
			printf("I don't know about the color %s.\n", choice);
		color_is_found = false;
		puts("Next color, please (empty line to quit):");
	}
	puts("Goodbye!");

	return 0;
}

char * s_gets(char * st, int n)
{
	char * ret_val;
	char * find;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n');   // look for newline
		if (find)                  // if the address is not NULL,
			*find = '\0';          // place a null character there
		else
			while (getchar() != '\n')
				continue;          // dispose of rest of line
	}
	return ret_val;
}


把enum spectrum color改爲了int color


在這裏插入圖片描述

共享名稱空間

在這裏插入圖片描述
在這裏插入圖片描述

typedef簡介

typedef和#define

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

其他複雜聲明(指針和數組的解釋※※重要)

複雜聲明:*、()、[]

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

函數類複雜聲明

在這裏插入圖片描述

typedef建立的聲明

在這裏插入圖片描述

函數和指針

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

// func_ptr.c -- uses function pointers
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define LEN 81

char * s_gets(char * st, int n);

char showmenu(void);
void eatline(void);     // read through end of line

void show(void(*fp)(char *), char * str);//函數指針

void ToUpper(char *);   // convert string to uppercase
void ToLower(char *);   // convert string to uppercase
void Transpose(char *); // transpose cases
void Dummy(char *);     // leave string unaltered

int main(void)
{
	char line[LEN];
	char copy[LEN];
	char choice;

	//函數指針
	void(*pfun)(char *); // points a function having a
						 // char * argument and no
						 // return value
	

	puts("Enter a string (empty line to quit):");

	while (s_gets(line, LEN) != NULL && line[0] != '\0')
	{
		while ((choice = showmenu()) != 'n')
		{
			switch (choice)  // switch sets pointer
			{
			case 'u': pfun = ToUpper;   break;
			case 'l': pfun = ToLower;   break;
			case 't': pfun = Transpose; break;
			case 'o': pfun = Dummy;     break;
			}
			strcpy_s(copy, line);// make copy for show()
			show(pfun, copy);  // use selected function
		}
		puts("Enter a string (empty line to quit):");
	}
	puts("Bye!");

	return 0;
}

char showmenu(void)
{
	char ans;
	puts("Enter menu choice:");
	puts("u) uppercase       l) lowercase");
	puts("t) transposed case o) original case");
	puts("n) next string");

	ans = getchar();    // get response
	ans = tolower(ans); // convert to lowercase
	eatline();          // dispose of rest of line
	while (strchr("ulton", ans) == NULL)
	{
		puts("Please enter a u, l, t, o, or n:");
		ans = tolower(getchar());
		eatline();
	}

	return ans;
}

void eatline(void)
{
	while (getchar() != '\n')
		continue;
}

void ToUpper(char * str)
{
	while (*str)
	{
		*str = toupper(*str);
		str++;
	}
}

void ToLower(char * str)
{
	while (*str)
	{
		*str = tolower(*str);
		str++;
	}
}
void Transpose(char * str)
{
	while (*str)
	{
		if (islower(*str))
			*str = toupper(*str);
		else if (isupper(*str))
			*str = tolower(*str);
		str++;
	}
}

void Dummy(char * str)
{
	// leaves string unchanged
}

void show(void(*fp)(char *), char * str)
{
	(*fp)(str); // apply chosen function to str
	puts(str);  // display result
}

char * s_gets(char * st, int n)
{
	char * ret_val;
	char * find;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n');   // look for newline
		if (find)                  // if the address is not NULL,
			*find = '\0';          // place a null character there
		else
			while (getchar() != '\n')
				continue;          // dispose of rest of line
	}
	return ret_val;
}

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

總結

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

關鍵概念

在這裏插入圖片描述

小結

在這裏插入圖片描述
在這裏插入圖片描述


2020-04-24 大連

(看的有些匆忙,可能太粗糙了,大多粘粘與截圖,這個結構自己之前參考別人的寫過,所有看這些理論能夠懂得更快一些,如果其他人看到,建議自己看看書或者教學視頻!!!)加油!!! 13章的文件處理個人可能不更,想學自己翻翻書!!!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章