【发布时间】:2016-03-09 17:19:11
【问题描述】:
#include<stdio.h>
#include<string.h>
struct employee
{
int id;
char name[20];
float salary;
char addr[];
}*emp;
void get_emp(struct employee **record)
{
printf("\tplease enter details of employee:\n");
printf("id= ");
scanf("%d",(*record)->id);//code to get input value
printf("Name= ");
scanf(" %s",(*record)->name);
printf("salary= ");
scanf("%f",(*record)->salary);
}
int main()
{
get_emp(&emp);
printf("id=%d\n",emp->id); // code to display the value
printf("Name=%s\n",emp->name);
printf("salary=%f\n",emp->salary);
return 0;
}
我有一个结构示例,我想将结构指针传递给函数,而不使用普通变量,而只使用指针。函数 get_emp(struct employee **record) 中的参数(双指针)是否是正确的方法,如果不应该进行哪些更改?另外如何在get_emp(struct employee **record)函数中获取用户的输入值以及如何显示该值?
【问题讨论】:
-
你还没有编译和运行这段代码有你。您正在取消引用一个空指针:
emp被初始化为 NULL(静态存储),它是一个指针。你没有在任何地方为结构分配内存,所以(*record)->id就像写(NULL)->id,这当然是错误的 -
我已经编译,代码没有任何问题。每当我尝试输入时,它就会停止工作并退出程序。