我正在尝试编写一个程序,以询问用户名和他们欠公司的钱。问题是,每次我尝试编译程序时,都会收到此警告,提示“用户可能未初始化”。我不太确定那是什么意思。对我做错了什么有帮助吗?谢谢!
#define END '\0'
#define LENGTH 20
struct info
{
char lastName[LENGTH];
float payment;
};
int customerS();
void accounts(struct info *userStart, int amount);
void changeNames(struct info *userStart, int amount);
void sort(struct info *userStart, int amount);
void results(struct info *userStart, int amount);
int main()
{
struct info *user;
int amount;
while((amount = customers()) != 0)
{
accounts(user, amount);
changeNames(user, amount);
sortNames(user, amount);
results(user, amount);
free(user);
}
return 0;
}
int customers()
{
int choice;
do
{
printf("\nHow many customers do you have (2 to 300, 0=quit): ",
scanf("%d", &choice);
}
while((choice < 2|| choice > 300) && choice != 0);
return choice;
}
void accounts(struct info *userStart, int amount)
{
struct info *user;
char *name[LENGTH];
float owed;
for(user = userStart; (user - userStart) < amount; user++)
{
scanf (" %s", name[LENGTH]);
getchar();
do
{
name[LENGTH] = getchar();
name[LENGTH]++;
}
while(!('\n'));
user->lastName[LENGTH + 1] = END;
scanf("%f", &owed);
user->payment = owed;
}
return;
}
void changeNames(struct info *userStart, int amount)
{
char *fast = &userStart->lastName[LENGTH],
*slow = &userStart->lastName[LENGTH];
if(tolower(*fast))
*slow++ = toupper(*fast);
while(*fast != END)
{
if(!isspace(*fast) || isalpha(*fast))
*slow++ = tolower(*fast);
fast++;
}
*slow = END;
return;
}
void sort(struct info *userStart, int amount)
{
struct info *user;
char *in,
*out,
temp;
for(out = user->lastName; (out - userStart->lastName) < amount; out++)
{
for(in = out + 1; (in - userStart->lastName) < amount; in++)
{
if(strcmp(user->lastName, userStart->lastName))
{
temp = *out;
*out = *in;
*in = temp;
}
}
}
return;
}
void results(struct info *userStart, int amount)
{
struct info *user;
printf("\nName: %s Payment: $%.2f", user->lastName, user->payment);
return;
}
答案 0 :(得分:0)
结构信息* user;
这会分配指向用户结构的指针,而不是结构本身。
因此,您可以通过struct info user;
在堆栈上分配它,并将&user传递给函数,或者通过user = (struct info*)malloc( sizeof(struct info) )
动态分配它。
这两种方法都应避免分段错误(因为指针未初始化)。 ..)