我正在尝试调用两个函数,第一个函数将接收数据,第二个函数将显示数据。但是我的第二个功能与第一个功能相同,所以它没有做我想做的事情。有什么建议吗?
struct Operador
{
char nome[32];
char telefone[15];
char idade[3];
};
struct Operador* fun( ) {
struct Operador* pItems = malloc( 3 * sizeof(struct Operador));
int n;
for(n=0;n<1;n++){
printf(" name: "); gets(pItems[n].nome);
printf(" telefone: "); gets(pItems[n].telefone);
printf(" age: "); gets(pItems[n].idade);
}
return pItems;
}
//*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*fim_operador
void lo()
{
struct Operador* pItems =fun();
int j;
printf("\n\n");
for(j=0;j<1;j++){
printf(pItems[j].nome);
printf(pItems[j].telefone);
printf(pItems[j].idade);
printf("\n\n");
}
free(pItems);
}
main()
{
fun();
lo();/ i want this function to simply display data
答案 0 :(得分:0)
代码中的一些更改:
下面我建议您使用的代码:
#include <stdio.h>
#include <stdlib.h>
struct Operador
{
char nome[32];
char telefone[15];
char idade[3];
};
struct Operador* fun() {
struct Operador* pItems = malloc(3 * sizeof(struct Operador));
//int n;
//for (n = 0; n<1; n++){
printf(" give nome: ");
scanf("%s", pItems->nome);
printf(" give telefone: ");
scanf("%s", pItems->telefone);
printf(" give age: ");
scanf("%s", pItems->idade);
//}
return pItems;
}
//*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-
void lo(void)
{
struct Operador* pItems = fun();
// int j;
printf("\n\n");
// for (j = 0; j<1; j++){ //no need for "for"?
printf("Name is: %s \n", pItems->nome);
printf("telefone is: %s \n", pItems->telefone);
printf("age is: %s \n", pItems->idade);
printf("\n\n");
// }
free(pItems);
return;
}
main()
{
//fun(); //no need to call, is called in lo()
lo();
}
输出是:
give nome: jim
give telefone: 2673673
give age: 32
Name is: jim
telefone is: 2673673
age is: 32
Press any key to continue . . .