結構體變數和結構體指標變數作為函式引數傳遞的問題
2015年8月28日13:20:28
通過函式完成對結構體變數的輸入和輸出
*/
# include <stdio.h>
# include <string.h>
struct Student
{
int age;
char sex;
char name[100];
}; //分號不能省
void InputStudent(struct Student *);
void OutputStudent(struct Student ss);
int main(void)
{
struct Student st; //15行
InputStudent(&st); //對結構體變數輸入 必須傳送st的地址
//printf("%d %c %s\n", st.age, st.sex, st.name);
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';
}
/*
//本函式無法修改主函式15行st的值 所以本函式是錯誤的
void InputStudent(struct Student stu)
{
stu.age = 10;
strcpy(stu.name, "張三"); //不能寫成 stu.name = "張三";
stu.sex = 'F';
}
*/