C 为什么函数指针不是';t被视为指向功能?

C 为什么函数指针不是';t被视为指向功能?,c,function,function-pointers,C,Function,Function Pointers,无法通过函数指针访问函数 我正在为美国和欧盟标准编写一个基于输入(体重指数计算器)的程序。 我的观点是使用一个函数“calcMethod”计算BMIndex,但尝试将另一个函数的指针分配给这个函数,会导致错误“调用的对象不是函数或函数指针”。感谢您的帮助 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> float calcEU(float

无法通过函数指针访问函数

我正在为美国和欧盟标准编写一个基于输入(体重指数计算器)的程序。 我的观点是使用一个函数“calcMethod”计算BMIndex,但尝试将另一个函数的指针分配给这个函数,会导致错误“调用的对象不是函数或函数指针”。感谢您的帮助

#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;
}
#包括
#包括
#包括
#包括
浮动计算器(浮动输入质量、浮动输入重量)
{
浮动BMIndexF;
BMIndexF=输入质量/(输入重量*输入重量);
返回BMIndexF;
}
浮点计算(浮点输入质量、浮点输入重量)
{
浮动BMIndexF;
BMIndexF=703*输入质量/(输入重量*输入重量);
返回BMIndexF;
}
int main()
{
浮点BMIndex、输入质量、输入重量;
浮动高度列,massRow;
浮动*计算方法;
int Mod=0;
int countRow,countColumn;
char unitStandard[2],metricUnitH[2],metricUnitM[2];
printf(“您的测量单位?欧盟(千克,米)或美国(磅,英寸)\n”);
获取(单位标准);
如果(strcmp(单位标准,“欧盟”)==0)
{
Mod=1;
strcpy(metricUnitH,“me”);
strcpy(公制单位m,kg);
浮动(*calcMethod)(浮动,浮动)=&calcEU;
}
否则,如果(strcmp(美国单位标准)=0)
{
Mod=-1;
strcpy(公制单位,“in”);
strcpy(公制单位m,“lb”);
浮动(*calcMethod)(浮动,浮动)=&calcUS;
}
其他的
{
printf(“错误输入”);
出口(-1);
}
printf(“介绍你的体重:\n”);
scanf(“%f”,&inputMass);
printf(“介绍您的身高:\n”);
scanf(“%f”&输入权限);
printf(“\n”);

对于(countRow=0;countRow而言,主要问题是在嵌套块中声明不同的
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;

您也不需要通过指针调用
*
,只需使用

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;
}
intmain()
{
浮点BMIndex、输入质量、输入重量;
浮动高度列,massRow;
float(*calcMethod)(float,float);//******此处更改1
int Mod=0;
int countRow,countColumn;
char unitStandard[2],metricUnitH[2],metricUnitM[2];
printf(“您的测量单位?欧盟(千克,米)或美国(磅,英寸)\n”);
获取(单位标准);
如果(strcmp(单位标准,“欧盟”)==0)
{
Mod=1;
strcpy(metricUnitH,“me”);
strcpy(公制单位m,kg);
calcMethod=&calcEU;/******此处更改2
}
否则,如果(strcmp(美国单位标准)=0)
{
Mod=-1;
strcpy(公制单位,“in”);
strcpy(公制单位m,“lb”);
calcMethod=calcUS;//******此处更改3
}
其他的
{
printf(“错误输入”);
出口(-1);
}
printf(“介绍你的体重:\n”);
scanf(“%f”,&inputMass);
printf(“介绍您的身高:\n”);
scanf(“%f”&输入权限);
printf(“\n”);

对于(countRow=0;countRow欢迎使用堆栈溢出。请将您的问题包含在整个错误消息中。同时指出导致错误的行。最后,制作您的代码示例,以便我们可以编译它并获得相同的错误。特别是,添加
main()
和所有必要的变量声明。
float(*calcMethod)(float,float)=&calcEU;}
变量在
}
之后停止存在。您在两个不同的
if
块中声明和初始化两个不同的
calcMethod
变量。在显示的代码中调用它时,两个声明都不在范围内。如果您声明一个
calcMethod
变量(位于第一个
if
语句上方)并在每个
if
语句中指定设计值,那么它应该可以工作。这与您的主要问题无关,但是:如果您希望用户提供两个字母的响应,请不要声明大小为2的数组来保存它,更重要的是,永远不要使用
get()
来读取它!(1)容纳长度为2的字符串所需的大小为3。(2)使用仅足以容纳所需字符串的数组是容易出错且毫无意义的,因为它不会阻止用户尝试键入更长的字符串。(3)函数
get
在病理学上是危险的,已从C语言标准中删除
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;
}