没有指针的分段错误?

时间:2013-03-24 01:13:40

标签: c pointers runtime-error

这是我的代码和问题。代码编译得很好。但是当我运行它。在getMenuOption()中输入菜单选项后,弹出“Segmentation Fault(core dumped)”。怎么了? 我是一般的编程新手。感谢您提供的帮助。

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

#define CALCULATE_PI 'a'
#define CALCULATE_GEOMEAN 'b'
#define CALCULATE_HARMMEAN 'c'

void printInstructions (void);
void printMenuOptions (void);
int  runMenuOption ();
int  getMenuOption ();
int  getLimit ();
int  calculatePi ();
int  calculateGeoMean ();
int  calculateHarmonicMean ();

int main(void)
{
    printInstructions();

    printMenuOptions();

    runMenuOption(getMenuOption());

    return 0;

}

void printInstructions (void)
{
    printf("======================================================\n");
    printf("=  PI, Geometric Mean, and Harmonic Mean Calculator  =\n");
    printf("=  Please refer to the menu to choose calucaltion    =\n");
    printf("=Choose desired menu option and press enter to begin =\n");
    printf("=      Proceed to follow on-screen instructions      =\n");
    printf("======================================================\n\n\n");

    return;
}

void printMenuOptions (void)
{
    printf("3 choices: Please enter a VALID letter.\n");
    printf("Choice 'a'   = Calcualtes PI\n");
    printf("Choice 'b'   = Calculates Geometric Mean\n");
    printf("Choice 'c'   = Calculates Harmonic Mean\n\n");

    return;
 }

int runMenuOption (int getMenuOption())
{
    char option;
    double answer,
            Pi = 0.0,
            geoMean = 0.0;

    option = getMenuOption();

    switch (option)
    {
        case CALCULATE_PI:
            calculatePi(getLimit());
            answer = Pi;
            break;
        case CALCULATE_GEOMEAN:
            calculateGeoMean(getLimit());
            answer = geoMean;
        case CALCULATE_HARMMEAN:
            printf("Harmonic Mean");
            break;
        default:
            printf("Incorrect Character!\n");
            printf("Try again");
            break;
    }

    printf("Your answer is %5p", &answer);

    return 0;
}

int getMenuOption (void)
{
    char option;

    printf("Please enter choice: ");

    scanf("%c", &option);

    return option;
}

int getLimit ()
{
    int limit;

    scanf("%d", &limit);

    return limit;
}

int calculatePi (void)
{
    int limit,
    count = 0,
    Pi = 0;

    printf("Please enter the PI limit: ");

    limit = getLimit();

    for (count = 1; count <= limit; count++)
    {
        Pi += 1 / count;
    }

    return sqrt(Pi * 6);
}

 int calculateGeoMean()
 {
    int limit,
        userValue = 0,
        count = 0;
    double geoMean = 0;

    limit = getLimit();

    while(count <= limit)
    {
       if (userValue <= 0)
           printf("Incorrect. Try again");
       else
       {
           count++;
           userValue *= userValue;
       }

    }
    geoMean = userValue;

    return sqrt(userValue);
}

int calculateHarmonicMean()
{
    int limit,
        userValue = 0,
        count = 0;
     double harmMean = 0;

     limit = getLimit();

    while(count <= limit)
    {
        if (userValue <= 0)
            printf("Incorrect. Try again");
        else
        {
            count++;
            userValue *= 1 / userValue;
        }

    }
    harmMean = userValue;

    return limit / userValue;
}

4 个答案:

答案 0 :(得分:6)

这个函数定义完全错误。

int runMenuOption (int getMenuOption())

您可以像这样传递getMenuOption的返回值

int runMenuOption (int option)

您不应将任何值传递给此函数,并在getMenuOption内调用runMenuOption。你这两个都做错了。

答案 1 :(得分:2)

int runMenuOption (int getMenuOption())

这是你的问题。

应该是:

int runMenuOption (int opt)

此外,您不应在getMenuOption()内拨打runMenuOption,因为您在将getMenuOption()作为参数传递给runMenuOption时正在呼叫runMenuOptionswitch应该只有{{1}}声明。

答案 2 :(得分:0)

您需要将功能定义从int runMenuOption (int getMenuOption())修改为int runMenuOption (int option)。在调用中,将调用getMenuOption()并将输出放入被调用函数的stack frame

答案 3 :(得分:0)

根据你对runMenuOption函数的声明,它需要一个指向函数的指针,该函数返回一个整数作为它的第一个参数:

int runMenuOption (int getMenuOption())

然后在这一行中调用该函数:

option = getMenuOption();

这很好。然而,问题在于这一行:

runMenuOption(getMenuOption());

在这里,您调用getMenuOption函数并将返回值传递给runMenuOption函数。但是你应该做的是将函数本身作为参数传递:

runMenuOption(getMenuOption);

您获得分段错误错误的原因是因为getMenuOption函数的返回值被视为函数指针,并且您的程序正在尝试调用该地址处的函数,这当然是无效。