3

I have a question regarding passing address of first element of an array to a (recursive) function:

selectionSort( &b[1], size-1);

When address is passed to a function, function parameter must be a pointer as I know. selectionSort method gets int b[] as argument. Not a pointer.

But code works without any problems. Also this code generates subarrays. When we pass the 1st element does it become the subarray's zeroeth element?

void selectionSort(int b[], int size)
{
    int temp,i;

    if (size>=1) 
    {
        for (i = 0; i < size; i++)
        {
            if (b[i]<b[0])
            {
                temp=b[0];
                b[0]=b[i];
                b[i]=temp;
            }
        }
        selectionSort( &b[1], size-1 );  
    }
}
Matt
  • 22,721
  • 17
  • 71
  • 112
Lyrk
  • 1,936
  • 4
  • 26
  • 48
  • "it became as subarrays zeroeth element?" - ***What?*** –  Jun 12 '13 at 12:33
  • 1
    Also, [arrays are not pointers](http://c-faq.com/aryptr/aryptr2.html), but they can [decay into one.](http://stackoverflow.com/questions/1461432/what-is-array-decaying). –  Jun 12 '13 at 12:34
  • Sorry for my English. I tried to say that, when original array's first elements address, is sent to function, this element becomes subarray's 0'th element? This is crazy – Lyrk Jun 12 '13 at 12:41

2 Answers2

4

Whenever you have a function like:

void f(int b[]);

this is the same as

void f(int *b);

The [] syntax for function parameters is just a special syntax to let the reader know that you intend to use the parameter like an array, but it is actually just a pointer. You can't pass an actual array to a function. Pointers and arrays work very similarly in C, so for the most part you can work with a pointer as if it were an array (but be careful -- there are important exceptions).

For example, when you access an element of an array like this:

array[index]

this is the same as

*(array+index)

and it doesn't matter whether array is a real array or just a pointer.

So &b[1] is the same as &*(b+1), which is the same as b+1.

If we were to write the code so that we see the b parameter as a pointer, it would look like this:

void selectionSort(int *b, int size){
    .
    .
    .

    selectionSort( b+1, size-1 );

    .
    .
    .
}

Now, you can see that selectionSort is being passed a pointer to an int, and it calls itself with a pointer to the next int.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
2

Yes, the code will create 'sub arrays' you should use:

A pointer to an array of integers

 selectionSort(int (*b)[], int size)

And call the function using:

selectionSort( &b[1], size-1 );  
Kauê Gimenes
  • 1,278
  • 1
  • 13
  • 30
  • 2
    If b is a type of int * or int[], then &b[1] is a type of int *, not int ** (int *[]), isn't it? – maverik Jun 12 '13 at 12:42
  • 2
    `(int* b[])` will break the logic (he's passing an array of integers, not an array of integer pointers), it works fine as it is at present. – Nobilis Jun 12 '13 at 12:43
  • 1
    int* b[]; = An array of int pointers. int (*b)[]; = A pointer to an array of integers – Kauê Gimenes Jun 12 '13 at 12:47
  • @Gorfi Nope, not fixed, if you insist on a pointer it can be modified to `selectionSort(int* b, int size)` – Nobilis Jun 12 '13 at 12:51
  • You sure he will not louse the array after the first run ? because its a recursive function, and will keep sending the array to itself – Kauê Gimenes Jun 12 '13 at 12:52