I will assume that int f(int[] a, int[] b)
is a typo: it should be int f(int a[], int b[])
.
First of all, arrays and pointers are different. Under many circumstances, the name of an array "decays" to a pointer to its first element, but not always. Otherwise, sizeof a
, for an array a
wouldn't "work".
Secondly, let's ignore recursion for a moment. Let's also make the function prototype simpler:
int g(int *a);
(I changed int a[]
to int *a
, because in this context, the name of an array is equivalent to a pointer.)
Now, you say that you may "dynamically allocate or resize" the array in the function call. Since everything is passed by value in C, a dynamic allocation or resizing of a
inside g()
cannot be seen by the caller: the caller would still have the old value of a
. But more importantly, if the "original" type of a
was an array, i.e., the caller had code like:
int data[SIZE];
g(data);
then trying to resize data
in g()
is bad, because the parameter specified in the call wasn't dynamically allocated to begin with. You can only dynamically resize something that was the result of malloc()
, calloc()
, or realloc()
.
So, there are two problems even with this simple definition:
- If
g()
has to dynamically resize the memory referred to by the address it is given, the value has to come from a dynamic allocation, not an array,
- After fixing that, since
g()
wants to be able to signal the change to the caller, you need to pass a pointer to what needs to be changed. So, the prototype of g()
becomes: int g(int **a);
.
Hopefully the above will help you get started. If you tell us more about your algorithm, in particular, what you mean by: "changing" and "writing", you will get better responses.
Edit: to answer your question in the comment:
So when I passed an array to a function it decays to a pointer and this pointer is passed by value. If that pointer is pointing a place I allocated before that call...
If you allocated something before the call, it never was an array to begin with. It can be indexed as an array, but that's not the point. So, maybe you are getting confused by the terminology here?
...when my new function changes the pointed value then that value is changed at caller, too.
The pointed-to value is changed, yes.
I dont want it to be like this, so I need a copy of the pointed value in the new function so that my original pointer's pointed value would not change. am I clear now?
It's clearer now, but then that raises more questions:
- If you are going to dynamically allocate or resize the data in each call to the function, how are you going to return those new pointers? You can't. And that means you have got yourself a memory leak. Unless you
free()
the data in the (recursively called) function itself.
- How would you resize the pointer? You may not be able to know the size of the data pointed to, unless you use a sentinel value.
Are you using the function to iteratively solve a puzzle or a problem? Are you free()
ing your data in each invocation of the function? If you can tell us, exactly what are you trying to do?