2

我已经编写了以下代码段,但无法理解为什么它无法到达最后一行 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;
        }
    }
}
4

2 回答 2

3

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

尝试选择不同的名称。

更新:我想我错了。标准头文件中定义的函数名称保留用作具有外部链接的标识符;#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)

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

于 2013-02-19T20:14:40.163 回答
0

您的 printf 没有出现,因为它没有从缓冲区中刷新,只需在字符串末尾使用“\n”,您就会看到它:

printf("Does not get here\n");

因此,错误不在scanf,而是在这一行:

walker = (*head).next;

正如我所看到的,程序可能会在head未分配时到达那里,因此您可以在函数的开头检查它:

void removeProduct(){
    int remove = 0;
    char kill_char = 'a';
    if (head == NULL) {
        printf("No product to remove!\n");
        return;
    }

我不确定是否还有其他错误,但这是我注意到的。

kill_char顺便说一句,您可以通过在格式字符串的开头和结尾插入空格来避免使用scanf

scanf(" %d ", &remove);

它将跳过所有白色字符(如制表符、空格和换行符)。而且,如果你真的只想跳过一个,而且只有一个,你可以使用*忽略匹配:

scanf("%d%*c", &remove);
于 2013-02-20T00:44:52.153 回答