S
S
Sergey2015-05-03 09:35:32
recursion
Sergey, 2015-05-03 09:35:32

How to use an array in recursion (C++)?

In a recursive function, I need to constantly interact with one array and change its contents depending on the results of the function, but the righteous compiler indicates that it is impossible to take and push the array into the function condition again like this.
I know that somehow it is possible to implement this using pointers, but I do not understand how. Explain, please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2015-05-03
@Adamos

You seem to be trying to pass the entire array in each function call.
This is not so easy to do, and, in fact, not what you need, because a copy will be transferred, and you want to change the original array.
The pointer is used like this:

int arr[32];
size_t index = 0;
recursive(&arr, index);
...
recursive(int **arr, index) {
  if(index < 32) {
    (*arr)[index] = 1;
    recursive(arr, ++index);
  }  
}

True, this is a primitive C-way.
In C++, instead of such an array, it is supposed to use a vector and pass it by reference.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question