What is the meaning of int[]
in C programming when you declare it? Like in this code
int findPivot(int[], int, int);
What is the meaning of int[]
in C programming when you declare it? Like in this code
int findPivot(int[], int, int);
Given that you are asking about function parameters of type int[]
, in this particular case it is equivalent to pointer to int
, i.e. int*
. There is no difference between the two. In other contexts, int[]
may have a different meaning.
These two function declarations are the same:
void foo(int[]);
void foo(int*);
And both are equivalent to this:
void foo(int[42]);
If you were to define more than one of them, you'd get a multiple definition error
void foo(int[]) {}
void foo(int*) {} // ERROR! Multiple definition.
Now, since C allows arrays to decay to pointers, you can actually invoke foo
with an array of int
as argument. The array decays to a pointer to its first element.
int a[42];
int b[99];
int c;
foo(a);
foo(b);
foo(&c);
Since the decay is to a pointer, all array size information is lost.
In the case you specified, it specifies that first position of that function with parameter stub int[]
is available to any array parameter
if its in an assignement like this
int values[] = {9, 2, 6, 1, 4};
it means values will be created with as many array items as added when that array is initialized, in this case 5.
In this function declaration
int findPivot(int[], int, int);
int[] - is a declaration of the function parameter as having type of an array of integers with unknown number elements. So it is an incomplete type.
The compiler adjusts this declaration to pointer. So these two function declarations are equivalent
int findPivot(int[], int, int);
int findPivot(int *, int, int);
When you pass an array as the corresponding argument of the parameter it is implicitly converted to a pointer to its first element. So the number of elements in the array is unimportant. Thus the above two declarations can be appended with a declaration of the function where the number of elements in the array is specified. For example
int findPivot(int[10], int, int);
int findPivot(int[20], int, int);
int findPivot(int[], int, int);
int findPivot(int *, int, int);
These declarations declare the same one function.
findPivot expects an int array and two int
variables. The int
array is effectively a set of integer values. It is equivalent to an int pointer in this case.
is a function declaration. At times, it is called a Forward declaration or Function prototype also.
FWIW, in a function declaration, you're allowed to omit the variable names, only specifying the variable type is enough.
In this particular context, an
int[]
andint*
carries the same meaning, i.e.,int[]
here refers that the function accepts a pointer to anint
as the first parameter .While calling the function, we can either pass
int
orint
array (because of array decaying effect while passing the argument, array type argument boils down to a pointer).You can call the function as