0
I want to make a function that copies a vector from double to another vector, allocating the new vector in the function that copies it.
That is the function:
void copiarvet(double *vet_origem, double *vet_destino, int n)
{
vet_destino = malloc(n*sizeof(double));
for(int i=0;i<n;i++)
vet_destino[i]=vet_origem[i];
}
That way, it doesn’t work at all. I did it differently, making the function return a pointer to double, instead of void, and that way it worked.
I would like to understand, why, when trying to do this way, already passing the address as parameter is not working.
And because it does not use https://en.cppreference.com/w/string/byte/memcpy?
– Maniero
What mistakes do you make when trying to compile?
– Jorge B.
@Jorge-b. Now compiled, it was a simple syntax error. But the function did not actually work.
– Gustavo Oliveira
@Maniero, no particular reason. I just wanted to know why the way I did didn’t work.
– Gustavo Oliveira
@Gustavooliveira the good programmer will only ask for help when exhausted attempts to find the error, not when appears the first error and instead of trying to figure out will ask.
– Jorge B.
Good afternoon, @Jorgeb. I appreciate the tip, but the syntax error was not what prompted me to ask the question. But yes, the function does not work correctly when passing the pointer by parameter.
– Gustavo Oliveira