Bootstrap

结构体变量和结构体指针变量作为函数参数传递问题

 字符串赋值:strcpy_s(pstu->name, "张三");

需要引入头文件:#include <string.h>

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

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

void InputStudent(struct Student * pstu) {   //pstu只占四个字节
	(*pstu).age = 10;
	strcpy_s(pstu->name, "张三");
	pstu->sex = 'F';
}
void OutStudent(struct Student ss) {
	printf("%d  %c  %s", ss.age, ss.sex, ss.name);
}

int main(void) {
	struct Student st;
	InputStudent(&st);
	printf("%d  %c  %s\n",st.age,st.sex,st.name);
	OutStudent(st);
	while (true){}
}

 

代码分析;

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

此处传递的是一个变量,此变量占的字节空间大,我们可以利用指针&#x

;