Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing pointer by reference in C

Tags:

c

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?

like image 875
umar Avatar asked Apr 20 '26 19:04

umar


2 Answers

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;
        }
    }
  } 
}
like image 110
dbush Avatar answered Apr 22 '26 22:04

dbush


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);
like image 40
Petar Velev Avatar answered Apr 22 '26 23:04

Petar Velev