2

Fairly new to c coding with background from c++. I have a simple program to sort an array using a function. I need to pass the int pointer by reference to the sort() function so that the compiler won't create a copy and after function call the array will be sorted. If I don't pass the reference, then after function ends the array will remain unsorted.

#include <stdio.h>
#include <stdlib.h>

void sort(int* & arr, int s, int e)
{
  int temp = 0, i, j;
  for (i=0;i<e;i++)
  {
    for (j=i+1;j<e;j++)
    {
        if (*arr[i]>*arr[j])
        {
            temp = *arr[i];
            *arr[i] = *arr[j];
            *arr[j] = temp;
        }
    }
  } 
}

int main()
{   
    int* arr = malloc(sizeof(int)*10);
    int i;

     for (i=0;i<10;i++)
         arr[i] = i+1;
     printf("Array before sorting:\n");
     for (i=0;i<10;i++)
         printf("%d  ", arr[i]);
     printf("\n");

     sort(arr, 0, 10);
     printf("Array after sorting:\n");
     for (i=0;i<10;i++)
         printf("%d  ", arr[i]);
     printf("\n");

     return 0;
}

I have also come to know that c doesn't allow pass by reference in a function, so how can I solve this issue?

dbush
  • 205,898
  • 23
  • 218
  • 273
umar
  • 551
  • 1
  • 6
  • 13
  • 8
    Why would you need to pass `arr` by reference? You never change it. – sepp2k Mar 28 '19 at 15:15
  • 4
    Pointers and arrays don't work the way you think they work. Pointers are not arrays. When you copy a pointer, an array is not copied. There is no harm in copying the pointer. You don't need to pass anything "by reference" here. – n. m. could be an AI Mar 28 '19 at 15:22

2 Answers2

7

In C, pass by reference is emulated by passing a pointer to the desired type. That means if you have an int * that you want to pass to a function that can be modified (i.e. a change to the int * is visible in the caller), then the function should accept an int **.

In your specific case however, this isn't what you want. You only need to pass an int * to your function which it can then dereference implicitly via the [] operator and change the elements of the array.

void sort(int *arr, int s, int e)
{
  int temp = 0, i, j;
  for (i=0;i<e;i++)
  {
    for (j=i+1;j<e;j++)
    {
        if (arr[i]>arr[j])
        {
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
  } 
}
dbush
  • 205,898
  • 23
  • 218
  • 273
3

There is no such thing as reference in C.

Passing a pointer to a function will not copy the object that the pointer is pointing to.

void foo(int* array);

You can then access the elements by deferencing the pointer.

int a = *array; //dereferencing the pointer. Getting the first element.
int b = array[0]; // does the same thing.
int c = *(array + 1) // second element. Pointer shifts by 1 * sizeof(type) bytes
int d = array[1] // second element again

Passing a pointer like this has the same overhead as passing an integer with the word size(since pointers are just integers underneath).

If you want to pass a pointer to an array you should pass it as a double pointer **. This will almost act as passing it as reference since you can change the pointer in the function.
Use case:

void foo(int** array) // passing a double pointer
{
    //....
    *array = theNewCollection;
    //...
}

Calling this function should be done as.

int* arr = malloc(sizeof(int)*10);
foo(&arr);
Petar Velev
  • 2,305
  • 12
  • 24