gpt4 book ai didi

scanf 上的 C 编程段错误

转载 作者:行者123 更新时间:2023-12-04 05:10:41 25 4
gpt4 key购买 nike

我编写了以下代码段,但无法理解为什么它无法到达最后的 printf 行。我在第 4 行之后立即得到一个段错误。 kill_char 只是用来杀死在前一个 scanf 中添加的“输入”字符。任何帮助将不胜感激,谢谢!

int remove = 0;
char kill_char = 'a';
printf("Enter the product number to be removed: ");
scanf("%d", &remove);
scanf("%c", &kill_char);
printf("Does not get here");

编辑:
完整代码如下,在removeProduct函数中报错
    #include <stdio.h>
#include <stdlib.h>

struct product_node {
char *supply_type;
long number;
char *description;
float price;
int quantity_bought;
float retail_price;
int quantity_sold;
struct product_node *next;
};

struct product_node *head;//declaring head out here
//allows the list to be in scope for the functions

/*Function Prototypes*/
void addProduct();
void removeProduct();
void listProduct();
void listSupplierTypes();
void supplierTypeProfit();
void totalProfit();

void addProduct(){
char kill_char = 'a';//used to kill the enter characters
struct product_node *new_node;
new_node = malloc(sizeof(struct product_node));

printf("\nEnter a string for type: ");
scanf( "%s", &(*new_node).supply_type);
scanf("%c", &kill_char);
printf("Enter the product number: ");
scanf("%ld", &(*new_node).number);
scanf("%c", &kill_char);
printf("Enter the description: ");
scanf("%s", &(*new_node).description);
scanf("%c", &kill_char);
printf("Enter the wholesale price: ");
scanf("%f", &(*new_node).price);
scanf("%c", &kill_char);
printf("Enter the quantity bought: ");
scanf("%d", &(*new_node).quantity_bought);
scanf("%c", &kill_char);
printf("Enter the retail price: ");
scanf("%f", &(*new_node).retail_price);
scanf("%c", &kill_char);
printf("Enter the quantity sold: ");
scanf("%d", &(*new_node).quantity_sold);
scanf("%c", &kill_char);

struct product_node *walker;
walker = head;
int can_insert = 1;
while (!(walker == NULL))
{
if (((*walker).number == (*new_node).number) && ((*walker).supply_type == (*new_node).supply_type))
{
can_insert = 0;
}
walker = (*walker).next;
}

if (can_insert==1)
{
(*new_node).next = head;
head = new_node;
printf("Insertion Successful");
}
else
{
printf("\nERROR INSERTING:This product name and number is already in the list\n");
}
free(new_node);
}
void removeProduct(){
int remove = 0;
char kill_char = 'a';
printf("Enter the product number to be removed: ");
scanf("%d", &remove);
scanf("%c", &kill_char);
printf("Does not get here");
struct product_node *walker;
struct product_node *prev;
prev = head;
walker = (*head).next;

if ((*prev).number == remove)
{
head = walker;
}//points head to second node to remove first

while (!(walker = NULL))
{
if ((*walker).number == remove)
{
(*prev).next = (*walker).next;
}
}
}
void listProduct(){
printf("Still unsure what defines a supplier...");
}
void listSupplierTypes(){
printf("Same as above");
}
void supplierTypeProfit(){
printf("Again");
}
void totalProfit(){
float total = 0.0;
struct product_node *walker;
walker = head;
while(!(walker == NULL))
{
total += ((float)(*walker).quantity_sold * (*walker).retail_price) - ((float)(*walker).quantity_bought * (*walker).price);
walker = (*walker).next;
}
printf("Total Profit is: $%.2f\n", total);
}

int main()
{
head = NULL;

char *temp_type;
char *temp_description;
int temp_number, temp_quantity_bought, temp_quantity_sold;
float temp_price, temp_retail_price;

while(!feof(stdin))
{
scanf( "%s %ld %s %f %d %f %d\n", &temp_type, &temp_number, &temp_description, &temp_price, &temp_quantity_bought, &temp_retail_price, &temp_quantity_sold);

struct product_node *new_node;
new_node = malloc(sizeof(struct product_node));
(*new_node).next = head;
head = new_node;

(*head).supply_type = temp_type;
(*head).number = temp_number;
(*head).description = temp_description;
(*head).price = temp_price;
(*head).quantity_bought = temp_quantity_bought;
(*head).retail_price = temp_retail_price;
(*head).quantity_sold = temp_quantity_sold;
}

freopen("/dev/tty", "rw", stdin);

int done=0;
int selection=0;

while (!done)
{
printf("\nMENU OPTIONS:\n");
printf("1. Add a product number\n");//Okay
printf("2. Remove a product number\n");
printf("3. List the products for a supplier\n");
printf("4. List all unique supplier types\n");
printf("5. Show profit margin for a specific supplier type\n");
printf("6. Show total profit\n");//Okay
printf("7. Quit\n");//Okay
printf("Enter a selection (1-7): ");

scanf("%d", &selection);
char garbage = 'a';
scanf("%c", &garbage);

switch(selection){
case 1:
addProduct();
break;
case 2:
removeProduct();
break;
case 3:
listProduct();
break;
case 4:
listSupplierTypes();
break;
case 5:
supplierTypeProfit();
break;
case 6:
totalProfit();
break;
case 7:
done = 1;
break;
default:
printf("Invalid selection.\n");
break;
}
}
}

最佳答案

remove 是标准函数的名称,声明于 <stdio.h> .定义您自己的对象或其他同名实体具有未定义的行为。调用可能试图存储 int remove() 的地址处的值功能。

尝试选择不同的名称。

更新:我想我错了。标准头文件中定义的函数名称保留用作具有外部链接的标识符;如果相关 header 是 #include,它们也被保留用作宏名称和文件范围的标识符。 d.两者都不应该适用于您的情况。不过,避免自己定义此类标识符仍然是一个好主意。

此外,这可能与您看到的症状无关,但是

scanf("%d", &obj);

如果输入是一个语法上有效的整数,其值超出 int 的范围,则具有未定义的行为.

执行确实到达了您的“未到达”行。您没有看到它,因为在程序终止之前没有打印缓冲输出。改变这个:
printf("Does not get here");

对此:
printf("Does not get here\n");
fflush(stdout);

当我在 gdb 下运行你的程序时,我在以下位置看到段错误:
if ((*walker).number == remove)

我在编译过程中也收到几个警告:
c.c: In function ‘addProduct’:
c.c:32:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
c.c:38:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
c.c: In function ‘main’:
c.c:134:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
c.c:134:9: warning: format ‘%ld’ expects argument of type ‘long int *’, but argument 3 has type ‘int *’ [-Wformat]
c.c:134:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘char **’ [-Wformat]

这很容易导致内存损坏。解决这些问题,看看会发生什么。

更新 2:

我不知道您的代码可能还有哪些其他程序,但是:
while (!(walker = NULL))
{
if ((*walker).number == remove)
{
(*prev).next = (*walker).next;
}
}

几乎肯定是错误的。您正在使用作业 =您可能需要相等比较的运算符 == .修复之后,代码会更清晰如下:
while (walker != NULL)
{
if (walker->number == remove)
{
prev->next = walker->next;
}
}

在 gdb 告诉我段错误在线 if ((*walker).number == remove) 后,当我快速查看时,这正是我突然想到的。 .

尝试自己使用调试器,一次解决一个问题,并注意任何编译器警告。

关于scanf 上的 C 编程段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14966217/

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