C语言结构体(struct)学习

结构初始化

#include<stdio.h>
#include<string.h>
char * s_gets(char * st, int n);
#define MAXTITL 41
#define MAXAUTL 31
//结构体 
struct book{
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
};

int main(void)
{
	struct book library;//把library声明为一个book类型的变量
	//初始化结构体 
	struct book b1 = {
		"The Greats Gyciby",
		"luo",
		22.8
	};
	//结构的初始化器
	struct book b2 = {
		.value = 19.23,
		.author = "James Bonds",
		.title = "007",
		"test",//对成员的最后一次赋值才是他真实的值,这里赋给author的值为“test”,因为在结构体声明中,title后面是author 
		666//同理,value会变成666 
	}; 
//	printf("Please enter the book title.\n");
//	s_gets(library.title, MAXTITL);
//	printf("Now enter the author.\n");
//	s_gets(library.author, MAXAUTL);
//	printf("Now enter the value.\n");
//	scanf("%f",&library.value);
//	printf("%s by %s:$%.2f\n", library.title, library.author, library.value);
	printf("%s by %s:$%.2f\n", b1.title, b1.author, b1.value);
	printf("%s by %s:$%.2f\n", b2.title, b2.author, b2.value);
	puts("Done!");
	
	return 0;	
} 

char * s_gets(char * st, int n){
	char * ret_val;
	int i = 0;
	
	ret_val = fgets(st, n, stdin);//如果一切进行顺利,返回的是第一个字符的地址 ,遇到文件结尾或者错误时返回NULL 
	if(ret_val){//如果输入成功 
		while (st[i] != '\n' && st[i] != '\0')//跳转到字符串末尾 
			i++;
		if(st[i] == '\n')//替换掉换行符 
			st[i] = '\0';
		else//如果输入字符串超出字数,之后的字符就省略 
			while (getchar() != '\n')
				continue;
	} 
	return ret_val;//输入正常则返回字符串首字符地址 
}

结构数组

//包含多本书的结构数组
#include<stdio.h>
#include<string.h>
char * s_gets(char * st, int n);
#define MAXTITL 41
#define MAXAUTL 31 
#define MAXBKS 100

//结构体 
struct book{
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
};

int main(void)
{
	struct book library[MAXBKS];//book类型的结构数组
	int count = 0;
	int index;
	
	printf("Please enter the book title.\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("%f",&library[count++].value);
			
			while (getchar() != '\n')
				continue;
			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?");
	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');//查找换行符
		if (find)//如果地址不是NULL 
			*find = '\0'; //在此书放置一个空字符 
		else
			while (getchar() != '\n')
				continue;  //处理输入行中其他字符
		
		 
	}
	
	return ret_val; 
	
}

嵌套结构

在一个结构中包含另一个结构

指向结构的指针

//指向结构的指针
#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] = {
		{
			{
				"Kobe","James"
			},
			"bananas",
			"basketball player",
			999999.0
		},
		{
			{
				"Owing","Walker"
			},
			"Apples",
			"basketball player",
			666666.0
		}
	};
	//声明 
	struct guy * him;//指向结构的指针
	
	printf("address #1: %p #2: %p\n", &fellow[0], &fellow[1]);
	
	//初始化指针 
	him = &fellow[0];//告诉编译器该指针指向何处
	//和数组不同的是,结构名并不是结构的地址,所以要加&符号
	
	/*
		用指针访问成员
		第一种是最常用的方法:使用->运算符
			如果him == &barney ,那么 him->income 就是 barney.income
		第二种方法:由于him是一个指针,可以用 *him 来提取him指向的结构
		barney.income == (*him).income == him->income 
	*/ 
	
	printf("him->income is $%.2f: (*him).income is $%.2f\n",him->income , (*him).income) ;
	him++;//指向下一个结构
	printf("him->favfood is %s: him->handle.last is %s\n",him->favfood , him->handle.last) ;
	 
	return 0;
}

结构和函数

//使用函数操作指向结构的指针
#include<stdio.h>
#include<string.h>

#define NLEN 30
struct namect {
	char fname[NLEN];
	char lname[NLEN];
	int letters;
}; 
struct pnames {
	char * f;
	char * l;
};
struct names {
	char f[100];
	char l[100];
};
/*
	函数不仅能把结构本身作为参数传递,还能把结构作为返回值返回 
*/
struct namect changeinfo (struct namect);
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;
	struct names n1 = {
		"Kobjmuhe","Jackjm,m "
	};
	struct pnames n2 = {
		"Kobe","Jack"
	};	
	/*
		n1结构总共要分配200个字节存储姓名
		而n2结构只存储了两个地址,所以在系统中占16个字节 
	*/
	printf("size of n1 = %u\n",sizeof(n1));
	printf("size of n2 = %u\n",sizeof(n2));
	getinfo(&person);
	makeinfo(&person);
	showinfo(&person);
	person = changeinfo(person);
	showstruct(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);
	printf("size = %u\n",sizeof(*pst));
}

struct namect changeinfo(struct namect info)
{
	 strcpy(info.fname,"best");
	return info;
}
void showstruct(const struct namect pst)
{
	printf("%s %s,your name contains %d letters.\n",pst.fname,pst.lname,pst.letters);
	printf("size = %u\n",sizeof pst);
}

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');//查找换行符
		if (find)//如果地址不是NULL 
			*find = '\0'; //在此书放置一个空字符 
		else
			while (getchar() != '\n')
				continue;  //处理输入行中其他字符			 
	}
	return ret_val; 	
}
//使用函数操作指向结构的指针
#include<stdio.h>
#include<string.h>
#include<stdlib.h>  //提供malloc(),free()等原型 

#define NLEN 30
#define SLEN 81
struct pnamect {
	char * fname;
	char * lname;
	int letters;
}; 

/*
	函数不仅能把结构本身作为参数传递,还能把结构作为返回值返回 
*/
void getinfo(struct pnamect *);
void makeinfo(struct pnamect *);
void cleanup(struct pnamect *);
void showinfo(const struct pnamect *);
char * s_gets(char *st, int n);

int main(void)
{
	struct pnamect person;
	getinfo(&person);
	makeinfo(&person);
	showinfo(&person);
	//malloc和free应成对使用,程序结束时释放内存 
	cleanup(&person);
	return 0;
}
//使用指针和malloc() 
void getinfo(struct pnamect * pst)
{
	char temp[SLEN];
	printf("Please enter your first name.\n");
	s_gets(temp, SLEN);
	//分配内存以储存名称
	pst->fname = (char *) malloc(strlen(temp) + 1);
	//把名称拷贝到动态分配的内存中
	strcpy(pst->fname,temp);
	printf("Please enter your last name.\n");
	s_gets(temp, SLEN);
	pst->lname = (char *) malloc(strlen(temp) + 1);
	strcpy(pst->lname, temp);
} 
void makeinfo(struct pnamect * pst)
{
	pst->letters = strlen(pst->fname) + strlen(pst->lname);
}
void showinfo(const struct pnamect * pst)
{
	printf("%s %s,your name contains %d letters.\n",pst->fname,pst->lname,pst->letters);
	printf("size = %u\n",sizeof(*pst));
}
void cheanup(struct pnamect * 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');//查找换行符
		if (find)//如果地址不是NULL 
			*find = '\0'; //在此书放置一个空字符 
		else
			while (getchar() != '\n')
				continue;  //处理输入行中其他字符			 
	}
	return ret_val; 	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SOC罗三炮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值