vevedashowstoppa
vevedashowstoppa

Reputation: 1

Incompatible Type for argument error

#include<stdio.h>
float functie(float vec[100],int dim2)
{
    int j;
    float suma,medie;
    for(j=1;j<=dim2;j++)
    {
        suma=suma+vec[j];
    }
    medie=suma/dim2;
    return medie;
}

int main()
{
    int dim ,i;
    float v[100],k;
    printf("Introduceti dimensiunea: ");
    scanf("%d",&dim);
    for(i=1;i<=dim;i++)
    {
        printf("v[%d]= ",i);
        scanf("%f",&v[i]);
    }
    k=functie(v[i],dim);
    printf("Date de iesire: %.2f",k);
    return 0;
}

I am trying to do the arithmetic average of a vector of "dim" elements,
using the function "functie"; but I am getting this error:

'incompatible type for argument 1 of 'functie'  problema3.c /problema3.c    line 23 C/C++ Problem'

The error is on the "k=functie(v[i],dim)" line.
What I did wrong?

Upvotes: 0

Views: 1877

Answers (3)

John Bode
John Bode

Reputation: 123468

In your call to functie, you're passing a single array element, not the entire array. That single element has type float, where the function is expecting a float * (pointer to float).

If you want to pass the entire array as an argument, just pass the array name (no subscript):

k = functie( v, dim );

Upvotes: 0

Selenium
Selenium

Reputation: 55

`k=functie(v,dim);`

when you want to transfer an array as a param you should transfer the original address of the array (it's means the name of array)

Upvotes: 0

Yunnosch
Yunnosch

Reputation: 26703

This

k=functie(v[i],dim);

calls functie with a float as first parameter, the float at position i of the array v.
Your function is declared as having a parameter of array type (which decays into a pointer to float).
So you are feeding a float into something which wants a pointer to float.
That is what the compiler tells you.

In order to fix you should review the reasons for having two very similar loops,
one in main, giving one float after another to functie,
one inside functie looping over all of the array, which it did not get.
Once you decided where you want to loop, you will suddenly find that solving the type conflict is easy.
Without knowing what you will want to do, giving a solution code is not possible.

There are other problems with your code, which are not in scope of your question.

E.g., to name a few:

  • access to array element one behind the size dim, though that only has an effect for dim=100
  • possible entering of too large arrays, because of lack of size check
  • scanf return value not checked
  • no recovery from non-number inputs

Upvotes: 1

Related Questions