You cannot assign to arrays in c, so this
int myGrades[2];
myGrades = initArray(myGrades);
is invalid, you simply cannot assign to an array but passing the array as a parameter should modify it inside the function as a pointer to the first element of the array is really passed, see the next point to understand why.
To fix this part, just remove the assignment
initArray(myGrades);
is enough.
You cannot define a function like this
int[] initArray(int arr[])
{
arr[0] = 78;
arr[1] = 86;
return arr;
}
Quoting this document, the C11 Standard Draft
6.7.6.3 Function declarators (including prototypes)
- A function declarator shall not specify a return type that is a function type or an array
type.
The []
syntax cannot be used for a function return type, also as a function parameter it will become a int
poitner anyway so using int arr[]
can be misleading, for example you can try to apply the sizeof
operator to it, and it will give you the size of a pointer.
Change it to
void initArray(int *arr)
{
arr[0] = 78;
arr[1] = 86;
}
and it should compile.
Although, the returned pointer is only valid in the scope where you defined arr
, in your program it's valid in main()
so pretty much everywhere if you pass arr
as a parameter to any function it will be fine. The point is that you don't need to return it at all since the original array (the one you passed to the function) is altered in place.