oj Problem F: 复杂类型作函数参数之结构体指针做参数

来源:互联网 发布:棋牌游戏辅助软件 编辑:程序博客网 时间:2024/06/06 12:27
题目:

Description

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

#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;
}

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

Input

Output

格式如下:

Sample Output

Lin Fang   20150305  F    98.00Xiang Jun  20150306  F    92.00
代码:
#include <stdio.h>#include <string.h>struct student                                                     /* 定义结构体类型 */{    char name[20];    long num;    char gender;    float score;};void modify(struct student *stu){    char str[20]="Xiang Jun";    strcpy((*stu).name,str);    (*stu).num=20150306;    (*stu).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;}
小结:
主要涉及到strcpy函数的使用。

0 0