1. 程式人生 > >C語言經典例題--結構體指標變數作為函式引數的傳遞

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;
}