站点图标 陌路寒暄

复杂类型作函数参数之结构体指针做参数

复杂类型作函数参数之结构体指针做参数

时间: 1ms        内存:128M

描述:

用结构体指针做参数,修改结构体变量的值。

#include <stdio.h>
#include <string.h>
struct student                                                     /* 定义结构体类型 */
{
    char name[20];
    long num;
    char gender;
    float score;
};

//begin


//end

int main( )
{
    struct student stu = {"Lin Fang", 20150305,  'F',  98.0 };  /* 定义结构体变量 */
    printf("%-10s %8d %2c %8.2f\n", stu.name, stu.num, stu.gender, stu.score);
    //将名字改为"Xiang Jun",num改为"20150306",score改为"92.0"
    modify(&stu);                                                  /* 调用函数change */
    printf("%-10s %8d %2c %8.2f\n", stu.name, stu.num, stu.gender, stu.score);
    return 0;
}

//只提交你编写的函数部分

输入:

输出:

格式如下:

示例输入:

示例输出:

Lin Fang   20150305  F    98.00
Xiang Jun  20150306  F    92.00

提示:

参考答案(内存最优[1088]):


#include <stdio.h>
#include <string.h>
struct student													 /* 定义结构体类型 */
{
    char name[20];
    long num;
	char gender;
    float score;
};
void modify(struct student *p)
{
    char a[20]="Xiang Jun";
    strcpy(p->name,a);
    p->num=20150306;
    p->score=92;
}
int main( )
{
    struct student stu = {"Lin Fang", 20150305,  'F',  98.0 };  /* 定义结构体变量 */
    printf("%-10s %8d %2c %8.2f\n", stu.name, stu.num, stu.gender, stu.score);
    //将名字改为"Xiang Jun",num改为"20150306",score改为"92.0"
    modify(&stu);                          						/* 调用函数change */
    printf("%-10s %8d %2c %8.2f\n", stu.name, stu.num, stu.gender, stu.score);
    return 0;
}

参考答案(时间最优[0]):


#include <stdio.h>
#include <string.h>
struct student													 /* 定义结构体类型 */
{
    char name[20];
    long num;
	char gender;
    float score;
};
void modify(struct student  *p)
{
    strcpy( p->name, "Xiang Jun" );
    p->num = 20150306;
    p->score = 92.0;
}
int main( )
{
    struct student stu = {"Lin Fang", 20150305,  'F',  98.0 };  /* 定义结构体变量 */
    printf("%-10s %8d %2c %8.2f\n", stu.name, stu.num, stu.gender, stu.score);
    //将名字改为"Xiang Jun",num改为"20150306",score改为"92.0"
    modify(&stu);                          						/* 调用函数change */
    printf("%-10s %8d %2c %8.2f\n", stu.name, stu.num, stu.gender, stu.score);
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

退出移动版