Function that copies vector to another, already passing the destination address as parameter

Asked

Viewed 26 times

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?

  • What mistakes do you make when trying to compile?

  • @Jorge-b. Now compiled, it was a simple syntax error. But the function did not actually work.

  • @Maniero, no particular reason. I just wanted to know why the way I did didn’t work.

  • @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.

  • 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.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.