`
dcj3sjt126com
  • 浏览: 1825622 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

C语言学习八结构体

    博客分类:
  • C
C 
阅读更多

为什么需要结构体,看代码

# include <stdio.h>

struct Student	//定义一个学生类型,里面有age, score, sex, 然后可以定义这个类型的变量
{
	int age;
	float score;
	char sex;
}

int main(void)
{
	struct Student st = {80, 66.6, 'F'};
/*
	int age;
	float score;
	char sex;
	int age2;
	float score2;
	char sex2;
*/

	return 0;
}

 

定义结构体

# include <stdio.h>

//第一种方式,最常用的是第一种,第二种第三种都不好,最好不要使用
struct Student
{
	int age;
	float score;
	char sex;
};

//第二种方式
struct Student2
{
	int age;
	float score;
	char sex
} st2;

//第三种方式
struct
{
	int age;
	float score;
	char sex
} st3;

int main(void)
{
	struct Student st = {80, 66.6, 'F'};

	return 0;
}

 

结构体的初始化和初值:

# include <stdio.h>

struct Student
{
	int age;
	float score;
	char sex;
};

int main(void)
{
	struct Student st = {80, 66.6, 'F'};	//初始化  定义的同时赋初值
	struct Student st2;	//这样没有赋值,因为已经定义好了,所以后面赋值只能单个赋值
	st2.age = 10;
	st2.score = 88;
	st2.sex = 'F';

	printf("%d %f %c\n", st.age, st.score, st.sex);
	printf("%d %f %c\n", st2.age, st2.score, st2.sex);
	
	return 0;
}

 

从结构体中取值示例:

# include <stdio.h>

struct Student
{
	int age;
	float score;
	char sex;
};

int main(void)
{
	struct Student st = {80, 66.6F, 'F'};	//初始化  定义的同时赋初值
	struct Student * pst = &st;

	st.score = 66.6f;	//第一种方法
	pst->age = 88;	//第二种方式  pst->age 在计算机内部会被转换成 (*pst).age, 没有为什么,
					//这就是->的含义,这是一种硬性规定
					// 所以 pst->age 等价于 (*pst).age 也等价于 st.age
					// 我们之所以知道 pst->age 等价于 st.age, 是因为pst->age 是被转化成了
					// (*pst).age来执行

	printf("%d %f\n", st.age, pst->score);
	
	return 0;
}

 

结构体复习练习:

# include <stdio.h>

struct Student 
{
	int age;
	char sex;
	char name[100];
};		//分号不能省

int main(void)
{
	struct Student st = {20, 'F', "小娟"};
	printf("%d %c %s\n", st.age, st.sex, st.name);

	struct Student *pst = &st;
	printf("%d %c %s\n", pst->age, pst->sex, pst->name);	// ps->age 转化成 (*pst).age 也等价于st.age
	
	return 0;
}

 

 通过函数对结构体的输入输出示例:

输出函数采用了发送内容的形式

/*
	2013年3月15日20:14:31
	功能:通过函数对结构体的输入输出
*/
# include <stdio.h>
# include <string.h>

struct Student
{
	int age;
	char sex;
	char name[100];
};	//分号不能省
//结构的参数传递

void InputStudent(struct Student *);
void OutputStudent(struct Student);

int main(void)
{
	struct Student st;	//15行

	InputStudent(&st);	//对结构体变量输入
	// printf("%d %c %s\n", st.age, st.sex, st.name);
	OutputStudent(st);	//对结构体变量输出

	return 0;
}

void OutputStudent(struct Student ss)
{
	printf("%d %c %s\n", ss.age, ss.sex, ss.name);
}

void InputStudent(struct Student * pstu)	//pstu 只占4个字节
{
	(*pstu).age = 10;
	strcpy( pstu->name, "张三");
	pstu->sex = 'F';
}

/*
//本函数无法修改主函数 15行st的值,所以本函数是错误的
void InputStudent(struct Student stu)
{
	stu.age = 10;
	strcpy(stu.name, "张三");	//不能写成 stu.name = "张三";
	stu.sex = 'F';
}
*/

 

输出程序调优,选择传地址的方式:

/*
	2013年3月15日20:14:31
	示例:
		发送地址还是发送内容

	发送内容传输的内容太多,需要复制很多的内容,比较消耗时间和资源,影响效率,发送地址就比较小

	指针的优点之一:
		快速的传递数据,
		耗用内存小
		执行速度快

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

struct Student
{
	int age;
	char sex;
	char name[100];
};	

void InputStudent(struct Student *);
void OutputStudent(struct Student *);

int main(void)
{
	struct Student st;	//15行
	//printf("%d\n", sizeof(st));

	InputStudent(&st);	//对结构体变量输入  必须发送st的地址
	OutputStudent(&st);	//对结构体变量输出	可以发送st的地址,也可以发送st的内容,但为了减少内在的耗费,也为了提高执行速度,推荐发送地址

	return 0;
}

void OutputStudent(struct Student * ss)
{
	printf("%d %c %s\n", ss->age, ss->sex, ss->name);
}

void InputStudent(struct Student * pstu)	//pstu 只占4个字节
{
	(*pstu).age = 10;
	strcpy( pstu->name, "张三");
	pstu->sex = 'F';
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics