-11

What is the meaning of int[] in C programming when you declare it? Like in this code

int findPivot(int[], int, int);
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
bigBunny
  • 73
  • 1
  • 2
  • 4

5 Answers5

4

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.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
2

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1
 int findPivot(int[], int, int);

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[] and int* carries the same meaning, i.e., int[] here refers that the function accepts a pointer to an int as the first parameter .

While calling the function, we can either pass

  • a pointer to an int or
  • an int array (because of array decaying effect while passing the argument, array type argument boils down to a pointer).

You can call the function as

#define MAX 128

int actualArray[MAX] = {0};
int p = <somevalue>;
int q = <someothervalue>;
//somecode 
int retval = findPivot(actualArray, p, q); 
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    Saying it accepts an `int` array as first parameter is correct but misleading. It accepts any pointer to `int`. – juanchopanza Jun 12 '15 at 14:09
  • Maybe just saying that the first parameter really is an `int*`, but it can accept an array argument because of array decay? – juanchopanza Jun 12 '15 at 14:17
  • OK, array *decay* is what lets you pass an array name as *argument* to a function with a pointer *parameter*. So `int[]` isn't `int*` because of decay. It is so because the type gets *adjusted* to pointer. Does that make sense? – juanchopanza Jun 12 '15 at 14:25
  • That's better! And there's no need for that "sir" business ;-) – juanchopanza Jun 12 '15 at 14:43
  • 1
    @juanchopanza Thanks. And regarding the "sir", it's [MMM](http://www.youtube.com/watch?v=HWYLFizhjHs). – Sourav Ghosh Jun 12 '15 at 14:51
0

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.

Gift Rise
  • 3
  • 4
-1

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.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175