为什么函数指针不被视为指向函数?

Why function pointer isn't seen as point to function?

无法通过函数指针获取函数。

我正在为美国和欧盟标准编写一个基于输入(体重指数计算器)的程序。 我的观点是使用一个函数 "calcMethod" 来计算 BMIndex,但是试图将其他函数的指针分配给这个函数会导致错误 "called object isn't a function or function pointer"。感谢任何帮助。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

float calcEU(float inputMass, float inputHeight)
{
    float BMIndexF;
    BMIndexF = inputMass / (inputHeight * inputHeight);
    return BMIndexF;
}

float calcUS(float inputMass, float inputHeight)
{
    float BMIndexF;
    BMIndexF = 703 * inputMass / (inputHeight * inputHeight);
    return BMIndexF;
}

int main()
{
    float BMIndex , inputMass , inputHeight;
    float heightColumn, massRow;
    float *calcMethod ;
    int Mod = 0;
    int countRow , countColumn;
    char unitStandard[2] , metricUnitH[2] , metricUnitM[2];

    printf("Your measure units? EU (kg, m) or US (lb, in) \n");
    gets(unitStandard);

    if(strcmp(unitStandard, "EU") == 0)
    {
        Mod = 1;
        strcpy(metricUnitH, "me");
        strcpy(metricUnitM, "kg");
        float (*calcMethod)(float , float) = &calcEU;
    }
        else if (strcmp(unitStandard, "US") == 0)
        {
            Mod = -1;
            strcpy(metricUnitH, "in");
            strcpy(metricUnitM, "lb");
            float (*calcMethod)(float , float) = &calcUS;
        }
            else
            {
                printf("Wrong Input");
                exit(-1);
            }

    printf("Introduce your body mass:\n");
    scanf("%f", &inputMass);

    printf("Introduce your height:\n");
    scanf("%f", &inputHeight);

    printf("\n");

    for(countRow = 0; countRow <= 5; countRow++)
    {
        for(countColumn = 0; countColumn <= 5; countColumn++)
        {
            heightColumn = inputHeight - 0.1 * (3 - countRow);
            massRow = inputMass - 1 * (3 - countColumn);

            if(countRow == 0 && countColumn == 0)  printf("H / M|");
            if(countRow == 0 && countColumn != 0)  printf("%.0f%s |", massRow , metricUnitM);
            if(countColumn == 0 && countRow != 0)  printf("%.1f%s |", heightColumn , metricUnitH);

            if(countRow != 0 && countColumn !=0)
            {
                //this line causes error
                BMIndex = (*calcMethod)(massRow , heightColumn);
                printf("%.2f |", BMIndex);
            }



        }

        printf("\n");
    }


    return 0;
}

注释行导致错误:被调用对象不是函数或函数指针

希望它不会抛出错误并按预期工作。

主要问题是您在嵌套块中声明了不同的 calcMethod 变量并更改了错误的变量。此外,您尝试调用的外部 calcMethod 变量甚至不是函数指针:

float *calcMethod ;

//assigning the pointer to function
//this subprogram is nested in main
if(Mod == 1) //Mod is just a variable
    {
        strcpy(metricUnitH, "me");
        strcpy(metricUnitM, "kg");

        // N.B. This is a different calcMethod variable!
        float (*calcMethod)(float , float) = &calcEU;
    } // N.B. calcMethod variable in previous block no longer exists!

if(Mod == -1)
    {
        strcpy(metricUnitH, "in");
        strcpy(metricUnitM, "lb");

        // N.B. This is a different calcMethod variable!
        float (*calcMethod)(float , float) = &calcUS;
    } // N.B. calcMethod variable in previous block no longer exists!

//the calcMethod is called
//this is nested in 2 for(s) and an if

// N.B. the calcMethod variable in this block is uninitialized, and
// it is not even a function pointer (it is a pointer to float) so
// this will not even compile....
BMIndex = (*calcMethod)(massRow , heightColumn);

解决方案是将外部 calcMethod 变量声明为函数指针,并将内部块更改为分配给外部块的 calcMethod 变量,而不是声明一个新变量:

float (*calcMethod)(float, float) = calcUS;  // default to "US"

//assigning the pointer to function
//this subprogram is nested in main
if(Mod == 1) //Mod is just a variable
    {
        strcpy(metricUnitH, "me");
        strcpy(metricUnitM, "kg");
        calcMethod = calcEU;
    }
if(Mod == -1)
    {
        strcpy(metricUnitH, "in");
        strcpy(metricUnitM, "lb");
        calcMethod = calcUS;
    }

//the calcMethod is called
//this is nested in 2 for(s) and an if
BMIndex = (*calcMethod)(massRow , heightColumn);

我将 calcMethod 初始化为 calcUS,以防 if 语句之一未设置它。或者,您可以将其初始化为 NULL 并在调用 calcMethod:

之前将其检查为错误条件
float (*calcMethod)(float, float) = NULL; // default to not set

// ...

if (calcMethod == NULL)
    {
        /* ERROR: calcMethod hasn't been set. */
        /* DO SOMETHING! */
    }
else
    {
        BMIndex = (*calcMethod)(massRow , heightColumn);
    }

问题在于您声明了 float *calcMethod; -- 指向浮点数的指针,而不是指向函数的指针。然后,您在内部块中将其重新声明为函数指针,但那只是在那些块中——您尝试调用它的地方就是在尝试调用浮点指针。

修复方法是首先将其声明为函数指针:

float (*calcMethod)(float, float);

然后当你决定使用哪个时,不要重新声明它,只需分配它:

calcMethod = calcUS;

calcMethod = calcEU;

您也不需要 * 来通过指针调用——您可以只使用

BMIndex = calcMethod(massRow , heightColumn);

您在 main() 函数中有三个 calcMethod 变量。第一个是指向浮点变量的指针(这显然不是函数指针)。另外两个是函数指针,但它们只存在于它们的代码块中。

如果您只将 calcMethod 定义为函数指针一次,那么无论代码中是否存在其他错误,都可以。

以下是三个变化:

int main()
{
    float BMIndex , inputMass , inputHeight;
    float heightColumn, massRow;
    float (*calcMethod)(float , float); // ****** CHANGE #1 HERE
    int Mod = 0;
    int countRow , countColumn;
    char unitStandard[2] , metricUnitH[2] , metricUnitM[2];

    printf("Your measure units? EU (kg, m) or US (lb, in) \n");
    gets(unitStandard);

    if(strcmp(unitStandard, "EU") == 0)
    {
        Mod = 1;
        strcpy(metricUnitH, "me");
        strcpy(metricUnitM, "kg");
        calcMethod = &calcEU; // ****** CHANGE #2 HERE
    }
        else if (strcmp(unitStandard, "US") == 0)
        {
            Mod = -1;
            strcpy(metricUnitH, "in");
            strcpy(metricUnitM, "lb");
            calcMethod = calcUS; // ****** CHANGE #3 HERE
        }
            else
            {
                printf("Wrong Input");
                exit(-1);
            }

    printf("Introduce your body mass:\n");
    scanf("%f", &inputMass);

    printf("Introduce your height:\n");
    scanf("%f", &inputHeight);

    printf("\n");

    for(countRow = 0; countRow <= 5; countRow++)
    {
        for(countColumn = 0; countColumn <= 5; countColumn++)
        {
            heightColumn = inputHeight - 0.1 * (3 - countRow);
            massRow = inputMass - 1 * (3 - countColumn);

            if(countRow == 0 && countColumn == 0)  printf("H / M|");
            if(countRow == 0 && countColumn != 0)  printf("%.0f%s |", massRow , metricUnitM);
            if(countColumn == 0 && countRow != 0)  printf("%.1f%s |", heightColumn , metricUnitH);

            if(countRow != 0 && countColumn !=0)
            {
                BMIndex = (*calcMethod)(massRow , heightColumn);
                printf("%.2f |", BMIndex);
            }
        }
        printf("\n");
    }

    return 0;
}