使用函数编辑struct

时间:2015-09-03 12:34:15

标签: c

我有一个结构:

typedef struct Employee{
    char name[MAX_SIZE];
    int salary;
    int experience;
} employee_t;

和一个主要功能:

void main()
{
    employee_t newEmployee={"",0,0}, tmp;

    newEmployee = readEmployee( &tmp ); //input for 5 employee's    
    printf("Employee name is %s, salary is %d, experience is %d years\n", newEmployee.name, newEmployee.salary, newEmployee.experience);
}

和另一个功能:

employee_t readEmployee(employee_t* emp)
{
    employee_t tmp;
    char name[MAX_SIZE];

    printf("Please enter Employee's name:(max %d chars)", 19);
    scanf("%s",&tmp.name);

    printf("Please enter Employee's salary:");
    scanf("%s",&tmp.salary);

    printf("Please enter Employee's experience:"); 
    scanf("%s",&tmp.experience);

    return tmp;
}

我想修改此函数中的结构并返回更新的结构。当我这样做时,该功能不会返回任何内容(在我的主要内容中不打印任何内容)。 我做错了什么?

3 个答案:

答案 0 :(得分:0)

您的scanf错了。像这样替换它们:

scanf("%s",tmp.name);

scanf("%d",&tmp.salary);

scanf("%d",&tmp.experience);

答案 1 :(得分:0)

在阅读薪水和经验时,您在scanf来电中使用了错误的格式说明符。这些字段的类型为int,因此您应该使用%d格式说明符。

答案 2 :(得分:0)

这是我的建议。但是你必须小心使用 <pre>you are assigning class name only for dropdown-toggle so it will work only one time for that you have to assign same class name to id (tobechanged) as well</pre> if (document.getElementById("tobechanged").className == "dropdown pull-right" ) { console.log('opening menu'); document.getElementsByClassName("dropdown-toggle").className = "dropdown pull-right open"; document.getElementById("tobechanged").className == "dropdown pull-right open"; } else { console.log('closing menu'); document.getElementsByClassName("dropdown-toggle").className = "dropdown pull-right"; document.getElementById("tobechanged").className == "dropdown pull-right" } 读取字符串。它可能导致缓冲区溢出。

scanf()