Can I pass an array by the value of a recursive function?

I want to write a recursive function that creates all possible solutions to the problem. I thought I needed to pass an array, and then at each recursive step set it to all possible values ​​in this recursive step, but then I started to wonder if this is possible, since C passes the array by passing a pointer. How do you usually deal with this?

I think about this. The array will take different values ​​depending on the selected path. Actually we would like to pass an array by value, I think.

recFunc(int* array, int recursiveStep) {
    for (int i = 0; i < a; i++) {
        if (stopCondition) {
            doSomething;    
        }
        else if (condition) {
            array[recursiveStep] = i;
            recFunc(array, recursiveStep+1);        
        }
    }
}
+2
source share
3 answers

You can pass an array by value by inserting it into the structure:

struct foo { int a[10]; };

void recurse(struct foo f)
{
    f.a[1] *= 2;
    recurse(f);    /* makes a copy */
}
+4

, . , , struct, , .

+3

Wrap it in a structure.

typedef struct arr_wrp {
    int arr[128]; // whatever
} arr_wrp;

void recFunc(arr_wrp arr, int step) {
    // do stuff, then
    arr.arr[step] = i;
    recFunc(arr, step + 1);
}
+3
source

All Articles