Im trying to make a function that adds one more element to my dynamic allocated array of struct but i cannot seem to make it work . This is a part of my code:
void add_vect(complex *vect,int n)
{ int i,pos;
complex *nr;
printf ("Please enter the number you want to add : ");
nr=read_complex(nr);
write_complex(nr);
printf ("Please enter the position of the new number : ");
scanf("%d",&pos);
n++;
vect=realloc(vect,n*sizeof(complex));
write_vect(n);
}
Here I'm reading the new complex number im trying to add to my array and the position it'll be added but when I realloc my array and I make a test to see if my old elements are still there, apparently it changes my last element.
so if i read 3 complex numbers
1 +2i , 3+4i, 5+6i
and i print my vector everything seems okay, but after I call this function and print it again after the realloc it shows:
v[1] = 1+2i - this one is good,
v[2] = 3+4i - still good,
v[3] = 121213121 +6i - not good and
v[4] = -12312321 +231312i
and I get that v[4] is like this because I did not modify it but why is v[3] affected by the realloc?