通过函数完成对结构体变量的输入输出


/*
    通过函数完成对结构体变量的输入输出 
*/
#include <stdio.h>
#include <string.h>

void InputStudent(struct Student *);
void OutputStudent(struct Student stu);
struct Student
{
    int age;
    char sex;
    char name[100];    
} ;//分号不能省略 


int main(void)
{
    struct Student st;
    InputStudent(&st);//对结构体变量输入 ,必须发送st的地址 
//    printf("%d %c %s\n", st.age,  st.sex, st.name); 
    OutputStudent(st);//对结构体变量输出 ,可以发送st的地址,也可以发送st内容 
    
    return 0;
}

void InputStudent(struct Student * pstu)//pstu只占4个字节 
{
    
    
    pstu->age = 10;
    strcpy(pstu->name, "张三");//不能写成 stu.name = "张三"
    (* pstu).sex = 'F'; 
} 

void OutputStudent(struct Student stu)
{
    printf("%d %c %s\n", stu.age,  stu.sex, stu.name);     
}


/*
//本函数无法修改主函数st的值 
void InputStudent(struct Student stu)
{
    stu.age = 10;
    strcpy(stu.name, "张三");//不能写成 stu.name = "张三"
    stu.sex = 'F'; 
} 
*/

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM