Using Functions with Arrays? Code doesn't work

时间:2018-03-25 19:02:20

标签: c arrays function

Why this code isn't work? I know there are other ways to do this but it must be this way. How can I fix this problem?

#include <stdio.h>
#include <stdlib.h>

int total(int arr[3], int size_of_array){

    int i=0;
    int total=0;

    for(i=0;i<size_of_array;i++){

        total+=arr[i];

    }

    return total;

}


int main(){


    int array2[3]= {0,1,8};

    int total(array2, 3);


}

1 个答案:

答案 0 :(得分:1)

int main(){
    int array2[3]= {0,1,8};
    int theAnswer;                           // Make an integer for the result.
    theAnswer = total(array2, 3);            // Call the function, and store the result.
    printf("The answer is %d\n", theAnswer); // Show the result to the user

    return 0;                                // main has to return 0 to indicate "success"
}