【发布时间】:2017-10-13 04:02:24
【问题描述】:
我编写了以下代码,我试图设置并通过 get 和 set 函数从结构中获取信息。但是,当我编译并运行程序时,它不会显示从输入中获得的信息。我的错在哪里?
#include <stdio.h>
#include <stdlib.h>
typedef struct Information{
int _id;
char* _name;
char* _family;
} Information;
void setInformation(Information* arg_struct){
printf("What is your name? ");
scanf("%s %s", arg_struct->_name, arg_struct->_family);
printf("What is your id? ");
scanf("%d", &arg_struct->_id);
}
void getInformation(Information* arg_struct){
printf("Your name is %s %s.\n", arg_struct->_name, arg_struct->_family);
printf("Your id is %d.\n", arg_struct->_id);
}
int main(int argc, char const *argv[]){
Information *obj = malloc(sizeof(Information));
setInformation(obj);
getInformation(obj);
return 0;
}
【问题讨论】:
标签: c pointers data-structures struct