jjj
jjj

Reputation: 11

C code Error not a function or function pointer

I have the following C code. When i try to compile it, i get an error message:

called object 'countPassed' is not a function or function pointer"

How may I fix this?

#include <stdio.h>

int main()
{
    int countWhoPassed, averageCalculate, printPassed;
    int amount = howManygrades();
    storesGrades(amount);
    printAll(amount);
    averageCalculate = averageCalc(amount);
    printAverage(amount);
    countWhoPassed = countPassed(amount);
    printPassed = printWhoPassed(amount);
    return 0;
}

int howManygrades()
{
    int gradesAmount;
    printf("How many grades do you want?\n");
    scanf("%d", &gradesAmount);
    while (gradesAmount > 10) {
        printf("Sorry, this amount of numbers is too big for the array.");
        scanf("%d", &gradesAmount);
        while (gradesAmount < 0) {
            printf("Sorry, this amount of numbers is too small for the array.");
            scanf("%d", &gradesAmount);
        }
    }
    while (gradesAmount < 0) {
        printf("Sorry, this amount of numbers is too small for the array.");
        scanf("%d", &gradesAmount);
        while (gradesAmount > 10) {
            printf("Sorry, this amount of numbers is too big for the array.");
            scanf("%d", &gradesAmount);
        }
    }

    return gradesAmount;
}

void storesGrades(int amount)
{
    int iterator;
    float grade;
    for (iterator = 0; iterator < amount; iterator++)
    {

        printf ("Enter another grade.");
        scanf("%f", &grade);
        while ((grade <= 0.0) || (grade > 10.0))
        {
            printf("Sorry, this number is not valid.");
            scanf("%f", &grade);
        }
        grades[iterator] = grade;
    }
}

void printAll(int amount)
{
    int iterator;
    for (iterator = 0; iterator < amount; iterator++)
    {
        printf("%.1f\n", grades[iterator]);
    }
}

int averageCalc(int amount)
{
    float sum, averageResult, average;
    int iterator;
    sum = 0.0;
    for (iterator = 0; iterator < amount; iterator++) {
        sum += grades[iterator];
    }
    average = sum / (float)amount;
    return average;
}

void printAverage(int amount)
{
    float printThisAverage = averageCalc(amount);
    printf("%.2f", printThisAverage);
}

int countPassed(int amount)
{
    int iterator, countPassing;
    countPassing = 0;
    for (iterator = 0; iterator < amount; iterator++)
    {
        if (grades[iterator] >= 5.5)
        {
            countPassing++;
        }
    }
    return countPassing;
}

int printWhoPassed(int amount)
{
    int returnValue = countPassed(amount);
    return returnValue;
}

Upvotes: 1

Views: 1095

Answers (2)

dhein
dhein

Reputation: 6555

Since the first time you use the function countPassed is here:

countWhoPassed = countPassed(amount);

And the function is being defined much later, the compiler can't know yet how to treat a function like that. So since no prototype is declared yet, the compiler doesn't know that countPassed() returns an int. So without that information, the compiler assumes,

countWhoPassed = countPassed(amount);

is trying to assign the function pointer of countPassed(int) to the variable countWhoPassed. But then it figures that it also doesn't know such function.

But how should the compiler know it better, not having seen this functions prototype yet?

So make the compiler aware of how that function should look like before its first occurrence.

So simply put

int countPassed(int amount);

in the declaration and include area so the compiler knows from there on how to treat that identifier.

Or as an advanced option, declare it in a header file and include that one.

Disclaimer: I haven't run the code nor checked it for other errors. I just answered that specific error you asked about.

Upvotes: 2

Blaze
Blaze

Reputation: 16876

There seem to be a couple of problems. I think the error message you get is weird, it should instead say that countPassed is not defined. That's because you define it after you call it. You can fix it by adding a prototype for the function before you call it. This way you tell the compiler about the existence of a function (without defining the function yet) and it will compile. For me, it worked by adding the following prototypes to your code before the main:

int countPassed(int amount);
void printAll(int amount);
void printAverage(int amount);
void storesGrades(int amount);

Alternatively, you could just put the main function after all the other functions. Of course, that's not always going to be an option in real projects.

Furthermore, there's no definition for grades. I assume you want an array of int, so adding something like int grades[10] should do the trick (in this example that would work with a maximum of 10 grades, it's also possibly to dynamically allocate that memory to allow whatever amount the user wants).

Also, I had to add #include "stdio.h" to make it compile with the printf, although some compilers won't mind if it's missing.

Upvotes: 2

Related Questions