Search code examples
arrayscimplicit-conversionfunction-declarationfunction-parameter

Passing arrays as arguments in C


I'm trying to make a function that identifies the maximum value in an array and calculate the sum of each time it appears. That's fine but the problem is that I need to make the function args the size of the array and the array itself.

This is what I've come up this far:

int sum(int a, int size)
{
    int i, max, output=0;

    //checking every index for max value
    for(i=0;i<=tam;i++){
        if(i==1){
            //setting max on the first index
            max=a[i];
        }else{
            if(a[i]>max){
                a[i]=max;
            }
        }
    }
    //making the sum
    for(i=0;i<size;i++){
        if(a[i]==max);
        output=output+max;
    }
    printf("%d", output);
}

The argument "a" is the array and the size is the size of the array. I get errors saying "a" is neither array nor pointer nor vector.

Any help is apreciated.


Solution

  • Replace int sum(int a, int size) to int sum(int *a, int size) or int sum(int a[], int size)