【问题标题】:C Programming segfault on scanfscanf上的C编程段错误
【发布时间】:2013-02-04 15:35:15
【问题描述】:

我编写了以下代码段,但无法理解为什么它无法到达最后一个 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 行是第一个 scanf 调用吗?
  • 是的,第一次 scanf 调用之后是段错误的来源。

标签: c segmentation-fault scanf


【解决方案1】:

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

尝试选择其他名称。

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

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

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 告诉我 segfault 在线 if ((*walker).number == remove) 后快速查看时,这就是我的想法。

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

【讨论】:

  • 我试过这个,但我认为错误也不在这里。我将 remove 的所有实例都更改为外卖,效果是一样的,只要我输入第一个 scanf 调用的值,就会出现段错误。
  • @WhatsInAName:你输入了什么值?
  • 只是个位数的整数,比如 3
  • 非常感谢您的帮助。我会尝试修复这些错误并回复您,让您知道我是否成功
  • 好的,我返回并修复了警告,但我仍然遇到了段错误。知道为什么此时它会出现段错误吗?
【解决方案2】:

你的 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;
    }

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

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

scanf(" %d ", &remove);

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

scanf("%d%*c", &remove);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    相关资源
    最近更新 更多