为什么调用此函数会产生错误“<function>不是函数或函数指针”?

时间:2016-09-18 21:07:32

标签: c function scope function-call

似乎函数qwertyInches()应该有效,但是当我在main()中调用它时它会给我

  

[错误]调用对象'qwertyInches'不是函数或函数指针。

非常感谢任何帮助。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Global constant
const int MAX_LENGTH = 81;   // global constant for max input line length


void qwertyInches (char row[], double *inches, int x, double y) {
    int d;
    for (d = 0; d < strlen(row); d++) {
        if (x == row[d]) {
            *inches = *inches + y;
        }
    }
}


int main() {
    int count[256] = { 0 };
    int  letterCounter = 0;
    int qwertyCounter = 0;
    int homeRowCounter = 0;
    int dvorakCounter = 0;
    char qwertyHomeRow[23] = {"asdfghjkl;ASDFGHJKL:\"'" };
    char dvorakHomeRow[22] = {"aoeuidhtns-_AOEUIDHTNS"};
    double percentOfCharQwerty = 0.0;
    double percentOfCharDvorak = 0.0;
    char qwertyHomeRowInches[4] = {"ghGH"};
    char qwertyRowInches[46] = {"qweruiopQWERUIOP{[}]\ZzXx|CcVvNnMm<,>./?"};
    char qwertyNumberInches[25]= {"`~1!2@3#4$5%7&8*9(0)-_=+)"};
    char qwertyTAndYInches[4] = {"TtYy"};
    char num6Inches[2] = {"6^"};
    char dvorakHomeRowInches[4]= {"iIDd"};
    char dvorakRowInches[41] = {"\"<',.>PpGgCcRrLl?/:+=|:;QqJjKkBb\MmWwVvZz"};
    char dvorakYandFInches[4] = {"YyFf"};
    char dvorakNumberInches [25] = {"~`1!2@3#4$5%7&8*9()0{[]}"};
    double dvorakInches = 0.0;
    double qwertyInches = 0.0;


    /* loop counters */
    int k;
    int l;
    int d;
    /* file handle --- in this case I am parsing this source code */
    FILE *fp = fopen("newsample.txt", "r");

    /* a holder for each character (stored as int) */
    int c;

    /* for as long as we can get characters... */
    while((c=fgetc(fp))) {

        /* break if end of file */
        if(c == EOF) {
            break;
        }
        else if (c == 32 || c == 10 || c == 9) {
            count[c]+=1;
        }
        /* otherwise add one to the count of that particular character */
        else {
            count[c]+=1;
            letterCounter++;
            for (l = 0; l < strlen(dvorakHomeRow); l++) {
                if (c == qwertyHomeRow[l]) {
                    qwertyCounter++;
                }
                if (c == dvorakHomeRow[l]) {
                    dvorakCounter++;
                }
            }
            qwertyInches(strlen(qwertyHomeRowInches) , &qwertyInches, c, .75 );

        }

    }


percentOfCharQwerty = (double) qwertyCounter / (double) letterCounter * 100;
percentOfCharDvorak = (double) dvorakCounter / (double) letterCounter * 100;

printf("Amount of Letters: %d\n", letterCounter);
printf("qwerty counter: %d\n", qwertyCounter);
printf("Dvorak counter: %d\n", dvorakCounter);
printf("Percent of qwerty letters %.2lf\n", percentOfCharQwerty);
printf("Percent of Dvorak letters %.2lf\n", percentOfCharDvorak);
printf("qwertyInches: %.2lf\n", qwertyInches);
printf("dvorakInches: %.2lf\n", dvorakInches);
/* close the file */
fclose(fp);
return;
}

3 个答案:

答案 0 :(得分:4)

qwertyInches内有一个main()局部变量,它会影响qwertyInches()的功能。

引用C11,章节§6.2.1,标识符范围强调我的

  

[....]如果标识符指定同名的两个不同实体   空间,范围可能重叠。如果是这样,一个实体(内部范围)的范围将结束   严格地在另一个实体的范围之前(外部范围)。 在内部范围内,   identifier指定在内部作用域中声明的实体;在外部宣布的实体   范围在内部范围内隐藏(并且不可见)。

解决方案: 更改其中一个名称。

也就是说,qwertyInches()函数的第一个参数应该是char *,但是你传递的是size_tstrlen()的输出),这是完全错误的。改变它。

答案 1 :(得分:2)

  

当我在main中调用它时它会给我这个... [错误]被称为对象'qwertyInches'不是函数或函数指针。

当然编译器是对的。 main() 内,此声明会影响函数声明:

double qwertyInches = 0.0;

因此,在main()中,qwertyInches指的是double类型的局部变量,而不是函数。

答案 2 :(得分:1)

您将qwertyInches定义为变量和函数。

void qwertyInches (char row[], double *inches, int x, double y) {

   double qwertyInches = 0.0;

只需更改上述其中一项的名称即可。我通常给我的函数一个“动作”名称,并给我的变量一个“事物”名称。