gpt4 book ai didi

c - 将 getchar() 的结果分配给结构成员指针

转载 作者:行者123 更新时间:2023-11-30 16:11:34 24 4
gpt4 key购买 nike

我一直在尝试创建一个函数,将 getchar() 分配给指向结构成员 (last_name) 的指针 (p_customers) ),但我不断收到编译器错误或段错误。这是我到目前为止所拥有的:

/* A customer account receivable record                               */
struct customer
{
char last_name[MAX_NAME_LENGTH+1]; /* Customer last name */
float amount; /* Amount the customer owes */
int priority; /* Customer priority */
};

void get_customers(struct customer *p_customers_start,
int number_customers)
{
struct customer *p_customers; /* Points to the account receivable */
/* customer */
char *p_last_name; /* Points to the last name character */

/* Loop processing account receivable customer data */
for (p_customers=p_customers_start;
(p_customers-p_customers_start) < number_customers;
p_customers++)
{
/* Print out account receivable customer */
printf( "\nCustomer #%d:",
*p_customers - *p_customers_start + 1);

/* Obtain last name of customer */
printf(" \nEnter the customer's last name: ");
getchar();
do
{
p_customers->last_name++ = getchar();
}
while(*p_last_name != NEW_LINE);
p_last_name = END_OF_STRING;
}
}

我不知道问题是什么,但有人可以告诉我如何解决它吗?

最佳答案

能贴出完整的代码吗?上面的代码太晦涩,无法给出任何正确分析的提示。但是,我可以根据假设做出以下评论: p_customers->last_name 和 p_last_name 可能不是指同一位置。结果,“last_name++ = getchar()”继续读取字符并尝试输入超出其限制的地址。这会导致段错误。

关于c - 将 getchar() 的结果分配给结构成员指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58576521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com