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?