This is a simple implementation of a sort algorithm. My question is. The array numbers, is declared and initialized in main. Then, I pass it like an argument in the function sort (Is a copy ?). Inside the sort function, numbers, now called array (a copy, as far as I know), is changed (sorted). So, why, after calling the function, the array numbers is changed (this is what I want, buy want to know why??. array scope is in sort, not main.
int main(void)
{
int numbers[SIZE] = { 4, 15, 16, 50, 8, 23, 42, 108 };
for (int i = 0; i < SIZE; i++)
printf("%d ", numbers[i]);
printf("\n");
sort(numbers, SIZE);
for (int i = 0; i < SIZE; i++)
printf("%d ", numbers[i]);
printf("\n");
return 0;
}
void sort(int array[], int size)
{
int swaps = 0;
while(swaps==0)
{
for(int i = 0; i < size ; i++)
{
for(int j = i + 1; j < size ; j++)
{
if( array[i] > array[j] )
{
// Swapping
int temp = array[i];
array[i] = array[j];
array[j] = temp;
swaps ++;
}
}
}
}
}
int array[]is the same asint *array. You are passing a pointer.