Is there any way to assign values to a vector faster?

Asked

Viewed 282 times

6

For example, I have the statement of the following vector:

float vet[3] = {1, 1, 1};

It would be possible to change all values of the same at once, without having to change one by one element or create a new vector and assign it to it:

vet[0] = -1;
vet[1] = 0;
vet[2] = 1;

Something more or less like this:

vet = {-1, 0, 1};

1 answer

3


Directly you don’t have no. You could create a function where you pass the parameters and it assigns each element. But that’s the opposite of being faster, at most it gets shorter.

This generally goes against the basic philosophy of C, although nothing prevents it from being used if it is the most suitable punctually.

  • In my case this is the most appropriate, because I have to change the value of the vector tens or hundreds of times. I created the function void createVector(float *vet, float e1, float e2, float e3) {
 vet[0] = e1;
 vet[1] = e2;
 vet[2] = e3;
}

  • There it makes sense, although the syntax shouldn’t be this.

  • Should it be like? Without the pointer? If that’s what I took, I was doing some tests here then I forgot to change to float vet[3] kk

  • That’s right. There are other ways to do it, but if it suits you, it’s suitable.

Browser other questions tagged

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