I have a (probably very basic) problem at modifying a pointer inside of a function. I want to pass a vector to a function. That function creates a similar vector (same size, type etc...) and then I want to assign this new vector to be my old one.
Here's some code to emulate what's happening
#include <stdio.h>
#include <stdlib.h>
void change(int *vec){
int i;
int vec2[10];
for(i=0;i<10;i++) vec2[i]=i+1;
vec=vec2;
}
int main(){
int i,ind[10];
for(i=0;i<10;i++) ind[i]=i;
for(i=0;i<10;i++) printf("%d ",ind[i]);
printf("\n");
change(ind);
for(i=0;i<10;i++) printf("%d ",ind[i]);
printf("\n");
return 0;
}
The output of this program just shows twice the original ind
vector
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
while I would like
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
Take note that this is not my actual code, just a much smaller version to show the problem. I know that in this version I could just modify vec
inside of the function and call it a day but in my original code I have to do the operations on another vector and at the end modify the original vector.