[Solved] Why does the error keep stating incompatible integer to pointer in C for an array?


Based on the phrasing of your question, I’m guessing that you are unfamiliar with the concept of pointers. In C, pointer is a type that stores the memory address of another variable. The & operator retrieves the memory address of a variable.

int i = 4;    // < An integer variable equal to 4
int* ip = &i; // < A pointer variable equal to the memory address of i

In C, arrays are handled as pointers to the first (0th) element of the array.

int a[] = {0, 1, 2, 3};
a == &a[0] // The name of the array is the same as the address of the first element

The confusion you’re having arises from the double-use of the [] operator. When used as a declaration, [] means a pointer (because arrays are the same as pointers). When used in an expression, [] accesses an element of an array.

Your method declaration asks for a parameter int values[] which, because it’s a declaration, means a pointer to an int. In your recursive call, you supply the argument nValues[(int)(n/2)+1] which, because it’s an expression, accesses the (n/2)+1th element of the array, which is an int, NOT a pointer to an int.

As a side note, n is an integer and 2 is an integer, so n/2 will also be an integer. There’s no reason to cast it.

1

solved Why does the error keep stating incompatible integer to pointer in C for an array?