/*
2009年11月24日9:17:43
示例:
发送地址还是发送内容
目的:
指针的优点之一:
快速的传递数据,
耗用内存小
执行速度快
*/
# 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)); //St 占108个字节 字节对其 4 100 4 内存对齐
InputStudent(&st); //对结构体变量输入 必须发送st的地址
OutputStudent(&st); //对结构体变量输出 可以发送st的地址也可以直接发送st的内容 但为了减少内存的耗费,也为了提高执行速度,推荐发送地址
return 0;
}
void OutputStudent(struct Student *pst) // 只发送 108 个字节的第一个字节的地址(首地址)
{
printf("%d %c %s\n", pst->age, pst->sex, pst->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';
}
*/