Passing array into function in c

arrays, c, function, prototype

Solution

This is obviously a recursive function. Note that `print_path()` takes 2 parameters: the first is an int array, and the second is an index to a position inside that array.

Calling it:

print_path( previous[desired_node_index] );

is absolutely wrong (unless you have overloaded this function), because it expects 2 parameters and you are only passing it one. What you should be doing is:

print_path( previous, desired_node_index );

What you seem to be missing in this function is an operation to increase/decrease the index variable, else you will always be printing the same position in the array.

Without knowing what is exactly that you are trying to do, there's the possibility that you wanted to do this:

print_path( previous, previous[desired_node_index] );

Problem

I'm getting gcc errors when I compile my code. The errors are about "passing argument 1 of ‘print_path’ makes pointer from integer without a cast". Here is my function prototype: ``` void print_path(int previous[], int desired_node_index); ``` Here is my function: ``` void print_path(int previous[], int desired_node_index) { if( previous[desired_node_index] != -1 ) print_path( previous[desired_node_index] ); printf("-> %d ", previous[desired_node_index]); } ``` and here is where I call my function: ``` print_path(previous, dest_index); ``` I'm obviously passing it in wrong, or else I'm doing something incorrectly about how to pass an array into a function. Any help? Thanks guys!

Original source