简体   繁体   English

如何修复代码中的分段错误以及将来修复此问题应遵循的过程是什么?

[英]How can i fix segmentation fault in my code and what is the process I should follow to fix this in the future?

I'm a beginner to C and came across a segmentation fault, which is what I understand as to be the program trying to access restricted or unavailable area of memory.我是 C 的初学者,遇到了分段错误,我理解这是试图访问 memory 的受限或不可用区域的程序。

This issue is occurring for me in a function:这个问题发生在我的 function 中:

void process_command(struct pizzeria *the_pizzeria, char *command) {
    if (command[0] == 'n') {
        struct order **last_next_order_field = get_last_next_order_field(the_pizzeria);
        *last_next_order_field = create_order();
    }

    if (command[0] == 's') {
        //Line below receives error
        get_integer_input(the_pizzeria->selected_order, "");
    }

}

which is linked to this other function:它链接到另一个 function:

void get_integer_input(int *variable, char *prompt) {
    printf(prompt);
    scanf("%d", variable);
    getchar();
}

It must be noted that these functions are relating to this struct:必须注意的是,这些函数与这个结构有关:

#define PIZZERIA_NAME_LENGTH 16
#define PIZZERIA_OWNER_LENGTH 16

struct pizzeria {
   char name[PIZZERIA_NAME_LENGTH];
   char owner[PIZZERIA_OWNER_LENGTH];
   int selected_order;
   struct order *orders;

}; 

I don't understand why when passing the_pizzeria->selected_order in get_interger_input in the function process_command makes pointer from integer without a cast as in my past functions of getting a char input instead I use the same process and don't receive an error.我不明白为什么在 function process_command中的get_interger_input中传递the_pizzeria->selected_order时,会从 integer 生成指针,而没有像我过去获取字符输入的函数那样进行转换,而是使用相同的进程并且没有收到错误。

I also set variable as a pointer so I don't understand the note produced by the terminal:我还将变量设置为指针,所以我不明白终端产生的注释:

note: expected 'int *' but argument is of type 'int'注意:预期为“int *”,但参数为“int”类型

void get_integer_input(int *variable, char *prompt) void get_integer_input(int *variable, char *prompt)

You don't show us these " past functions ", but I assume that they ask the user for name or owner.您没有向我们展示这些“过去的功能”,但我假设他们向用户询问姓名或所有者。 These elements of your struct are arrays, and if you pass their name as an argument it decays to a pointer to the first element.您的结构的这些元素是 arrays,如果您将它们的名称作为参数传递,它会衰减为指向第一个元素的指针。

In contrast, the integer is not an array.相比之下,integer 不是数组。 Therefore, you need to pass its address like this:因此,您需要像这样传递它的地址:

    if (command[0] == 's') {
        get_integer_input(&the_pizzeria->selected_order, "");
    }

If you are not very firm with operator precedence, better use parentheses:如果您对运算符优先级不是很严格,最好使用括号:

    if (command[0] == 's') {
        get_integer_input(&(the_pizzeria->selected_order), "");
    }

The compiler is correct with its diagnostic message.编译器的诊断消息是正确的。 You pass an integer, but the functions needs a pointer.你传递了一个 integer,但函数需要一个指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM