0

I just wrote some code for a bubble sort function. I would like to test it using a simple printf with an array of values passed into it but am struggling. For now, let's assume that this file is independent (not linked to any header as implementation). Please see code and error message below.

include

include

int main(int values[]) { int n = 5 ; values[] = {3,4,1,5,2} ; bool iter ; // O(n^2) sorting algorithm do { iter = false ; for (int i = 0; i < n-1 ; i++) { if (values[i] > values[i + 1]) { int temp_val = values[i] ; values[i] = values[i + 1] ; values[i + 1] = temp_val ; iter = true ; } } } while (iter == false) ; return ;

// prints the array to test if the function is working correctly
for (int i = 0; i < n ; i++)
    {
        printf("%d, ", values[i]) ;
    } 
    printf("\n") ;

}

THE ERROR THAT I GET IS: error: first parameter of 'main' (argument count) must be of type 'int' int main(int values[]) ^ test.c:16:12: error: expected expression values[] = {3,4,1,5,2} ; ^ test.c:16:16: error: expected expression values[] = {3,4,1,5,2} ; ^ test.c:33:5: error: non-void function 'main' should return a value [-Wreturn-type] return ;

1 Answer 1

0

You can't declare main as int main(int values[]). Main must be either int main(void) or int main(int argc, char* argv[])

I'd suggest you have int main(void) and the rest of your code should still work.

3
  • Thanks @curiouskiwi. I used int main void and still got an error. I think it has to do with the output returning something that is not specified in main. Here is the error : non-void function 'main' should return a value [-Wreturn-type] return ; ^ Commented Sep 11, 2016 at 20:08
  • When you do int main(void) the void means no inputs, but the int means that it is going to return an integer - usually 0 for success and a non-zero number as an error message. That means that every return needs a number, like return 0. It also means that the program can't end without returning an int. And, no, you can't change the int.
    – Cliff B
    Commented Sep 11, 2016 at 20:23
  • Thanks @CliffB . I got the programme to return 0 and all is fine now. Commented Sep 11, 2016 at 20:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .