为什么收到分段错误:11

时间:2018-11-09 07:56:41

标签: c function pointers segmentation-fault

该程序应充当复数计算器。我需要将这些功能与给定参数一起使用。运行程序时,选择操作后,我可以输入4个数字。在按回车键输入第四个数字之后,我立即收到一个错误,称为“分段错误:11”。我不知道为什么收到此消息。

我已经包含了代码。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void read_num(float *real_part, float *imaginary_part) {
    float realnumberholder, imagnumberholder;
    printf("Please type in the real component: ");
    scanf("%f", &realnumberholder);
    *real_part = realnumberholder;
    printf("Please type in the imaginary component: ");
    scanf("%f", &imagnumberholder);
    *imaginary_part = imagnumberholder;
}

void read_nums(float *real_part_1, float *imaginary_part_1,
               float *real_part_2, float *imaginary_part_2)   {
    printf("Reading the first imaginary number...");
    read_num(real_part_1, imaginary_part_1);
    printf("Reading the second imaginary number...");
    read_num(real_part_2, imaginary_part_2);
}

void print_complex(float real_part, float imaginary_part) {
    printf("The operation yields %6.3f + %6.3fi", real_part,
           imaginary_part);
}

void add(float real_part_1, float imaginary_part_1, float
         real_part_2, float imaginary_part_2, float *real_result, float
         *imaginary_result) {
    read_nums(&real_part_1, &imaginary_part_1, &real_part_2,
              &imaginary_part_2);
    *real_result = (real_part_1 + real_part_2);
    *imaginary_result = (imaginary_part_1 + imaginary_part_2);
    print_complex(*real_result, *imaginary_result);
}

void subtract(float real_part_1, float imaginary_part_1, float
              real_part_2, float imaginary_part_2, float *real_result, float
              *imaginary_result) {
    read_nums(&real_part_1, &imaginary_part_1, &real_part_2,
              &imaginary_part_2);
    *real_result = (real_part_1 - real_part_2);
    *imaginary_result = (imaginary_part_1 - imaginary_part_2);
    print_complex(*real_result, *imaginary_result);
}

void multiply(float real_part_1, float imaginary_part_1, float
              real_part_2, float imaginary_part_2, float *real_result, float
              *imaginary_result) {
    read_nums(&real_part_1, &imaginary_part_1, &real_part_2,
              &imaginary_part_2);
    *real_result = (real_part_1 * real_part_2);
    *imaginary_result = (imaginary_part_1 * imaginary_part_2);
    print_complex(*real_result, *imaginary_result);
}

int main()
{
    int option;
    float realone = 0;
    float imaginaryone = 0;
    float realtwo = 0;
    float imaginarytwo = 0;
    float *realresult;
    float *imaginaryresult;


    printf("\nComplex Number Arithmetic Program.\n");
    printf("1) Add two complex numbers\n2) Subtract two complex
           numbers\n3) Multiply two complex number\n4) Quit\nChoose an
           option (1-4): ");
    scanf("%d", &option);

    while(option != 4) {

        switch(option) {
        case 1 :
            add(realone, imaginaryone, realtwo,
                imaginarytwo, realresult, imaginaryresult);
            printf("1) Add two complex numbers\n2)
                   Subtract two complex numbers\n3) Multiply
                   two complex number\n4) Quit\nChoose an
                   option (1-4): ");
            scanf("%d", &option);
            break;
        case 2 :
            subtract(realone, imaginaryone, realtwo,
                     imaginarytwo, realresult, imaginaryresult);
            printf("1) Add two complex numbers\n2)
                   Subtract two complex numbers\n3) Multiply
                   two complex number\n4) Quit\nChoose an
                   option (1-4): ");
            scanf("%d", &option);
            break;
        case 3 :
            multiply(realone, imaginaryone, realtwo,
                     imaginarytwo, realresult, imaginaryresult);
            printf("1) Add two complex numbers\n2)
                   Subtract two complex numbers\n3) Multiply
                   two complex number\n4) Quit\nChoose an
                   option (1-4): ");
            scanf("%d", &option);
            break;
        default:
            printf("Please input a valid menu
                   option\n");
            printf("1) Add two complex numbers\n2)
                   Subtract two complex numbers\n3) Multiply
                   two complex number\n4) Quit\nChoose an
                   option (1-4): ");
            scanf("%d", &option);
            break;
        }
    }

    if (option == 4) {
        printf("Bye!\n");
        return 0;
    }

}

2 个答案:

答案 0 :(得分:4)

在主函数中将realresult和imaginaryresult用作指针。它们应该只是float变量,您应该将这些变量的地址传递给相应的函数。更改以下行

float *realresult;
float *imaginaryresult;

float realresult;
float imaginaryresult;

此外,需要将函数调用修改为

add(realone, imaginaryone, realtwo, imaginarytwo, &realresult, &imaginaryresult);
...
subtract(realone, imaginaryone, realtwo, imaginarytwo, &realresult, &imaginaryresult);
....
multiply(realone, imaginaryone, realtwo, imaginarytwo, &realresult, &imaginaryresult);

答案 1 :(得分:3)

更改此

float *realresult;
float *imaginaryresult;

对此:

float realresult;
float imaginaryresult;

然后更改函数调用以改用地址:

add(realone, imaginaryone, realtwo,
            imaginarytwo, &realresult, &imaginaryresult);

(请注意&符号)

它将起作用。就像以前那样,如果没有这些内存分配,那两个指针就不会指向任何有效的内存。它们是未初始化的,像您一样访问它们是未定义的行为。实际上,通常会尝试将垃圾值解释为一个地址并尝试访问该地址,这会导致分段错误。