C语言经典例题--结构体指针变量作为函数参数的传递

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

struct student {
	int age;
	char sex;
	char name[30];
};

void inputstudent(struct student *ps)//对结构体变量输入时必须传地址
{
	(*ps).age = 10;
	strcpy(ps->name, "张三");
	ps->sex = 'f';
}

void outputstudent(struct student *ps)//对结构体变量输出时,可传地址也可传内容,但为了减少内存耗费,提高运行速度,建议使用传值
{
	printf("%d %c %s\n", ps->age, ps->sex, ps->name);
}
int main()
{
	struct student st;
	inputstudent(&st);
	outputstudent(&st);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42386328/article/details/81392074