0

OK -

I am working on a snippet of code to make a recursive function that sorts values. I have kept banging on the code trying to make it work as I want it too. The code now runs and works as expected - but it is bothering me that I don't really know why my code is working... I think I know why it is working but I wanted to check with someone who can confirm my thinking....

I made a couple of functions - one was a recursive function to sort and the second function was to print out an array. In each function call I pass the entire array and an integer value. When I first tried to pass the array I would put brackets in and array count in.. and clang would puke. I kept trying different syntax combinations until I could pass the array by just giving the function the name of the array and the name of the array count variable.

My question is as follows - when I pass the entire array am I only passing a pointer to the location where the array is? How does C know I am passing the array by reference and not passing by value?

After looking at my code - I can only conclude that I am passing by reference because the operations I do on the array "take hold" in every iteration of the recursion process... but

When I am passing the array to the function my code looks like the following...

int array_size = 6;
int array_list[] = {23, 42, 4, 16, 8, 15};

//Print out Array
print_Array(array_list,array_size);

//Call recursive function to sort arrays: 
fn_sort_integers(array_list,array_size);

//Print out Array
print_Array(array_list,array_size);


void fn_sort_integers(int input[], int n)
{
//Sorting done here
//Recursive call to fn_sort_integers
}


void print_Array(int input[], int n)
{
//Print Array
}
1
  • Thanks very much.... I changed int input[] to int *input and it worked perfectly! void fn_sort_integers(int *input, int n) - so see will let me pass around arrays to functions rather easily! That raises the question - can I pass an array instantiated on the stack just was easily as an array that is instantiated on the heap using malloc?..... hmmmmmmm – Myriddin Mar 27 '15 at 16:16
1

when I pass the entire array am I only passing a pointer to the location where the array is?

Yes.

How does C know I am passing the array by reference and not passing by value?

In C, technically you are always passing by value (aka by copy). But probably you can't pass a whole copy of an array to a function, unless you do that manually or something.

What basically happens is that the name of an array decays to a pointer to the array when used in place of a pointer of the array elements' type. And what is really passed is a copy of that pointer. Ex.

// some code
int x[] = {1, 2, 3};
foo(x);
// some code

void foo (int arr[]) // could be (int *arr) instead
{
    // some code
}

What happens is that a copy of the address that x holds is copied into arr. This is the reason why when you're modifying the contents of an array that is passed to a function, these modifications affect the original array outside of the function.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .