C++ Change Function's Variable Argument

How to change value of variable passed as argument?

You're wanting to change where a char* points, therefore you're going to need to accept an argument in foo() with one more level of indirection; a char** (pointer to a char pointer).

Therefore foo() would be rewritten as:

void foo(char **foo /* changed */, int baa)
{
if(baa)
{
*foo = "ab"; /* changed */
}
else
{
*foo = "cb"; /* changed */
}
}

Now when calling foo(), you'll pass a pointer to x using the address-of operator (&):

foo(&x, 1);

The reason why your incorrect snippet prints baa is because you're simply assigning a new value to the local variable char *foo, which is unrelated to x. Therefore the value of x is never modified.

c++ change function's variable argument

In order to change the arguments, you would have to take references:

bool verifyStudent(string& id, string& name, int& grade, int& points, string& type) 

Although I'd say that function is not verifyStudent as much as verifyAndCorrectStudentIfNeeded.

How to pass arguments in C so that values can be changed?

By default C will create a copy of the argument and operate on it inside the function, the copies cease to exist as soon as the function ends.
What you want to do is use pointers in your arguments and dereference them to operate on the value the pointers point to.

This topic may help you understand better: What's the difference between passing by reference vs. passing by value?

This should work:

void go_south_east(int *lat, int *lon) {
*lat = *lat - 1;
*lon = *lon + 1;
}

int main() {
int latitude = 32;
int longitude = -64;
go_south_east(&latitude, &longitude);
printf("Now at: [%i, %i]\n", latitude, longitude);
}

Update (int) variable in C inside a function

stage is passed by value to changeStage. stage = 1 only changes the local value of stage in changeStage, not the value of stage in main. You have to pass a pointer instead:

while (stage != 0) {
changeStage(&stage);
}

void changeStage(int *stage) {
*stage = 1;
}

is correct to change argument value inside function

It is correct in this particular case, and it has no side effect since the argument here is a copy of the actual parameter passed.

But it might make a trouble in general case, if you unintentially change parameter passed by reference.

Also modifying parameters usually make the code worse readable. And might require more time for understanding what is really happening in the function.

So, generally I wouldn't recommend modifying parameters. Leave this optimization to the compiler.

Difference between modifying a function parameter and modifying a local variable

Although in this case declaring a new variable which holds the pointer to the list makes no difference, I would say that this is a good practice to never mutate the function parameters, only when the function's nature requires to do so (i.e. when you have an inout parameter, like a buffer). This unwritten rule will save you from a lot of problems in future when dealing with more complex functions.

Consider just a simple case where you would need for some reason to also destroy and free the contents of this list after applying the f function. Of course, in this simple example you would easily spot the issue, but there are cases when this wouldn't be that obvious.

So, again, even though here it makes no difference, I strongly recommend to always write your functions using this pattern and this will avoid a lot of weird and unexpected issues.

How to assign variadic/variable arguments in C++

Instead of using C's va_ stuff, C++ has it's own variadic template arguments, which you should preferably use

I'm no expert on this, but it could look a little bit like

#include <vector>
#include <iostream>

template <typename... Arg>
void set_params(const std::vector<double> &input, Arg&... arg) {
unsigned int i{0};
(
[&] {
if (i < size(input)) {
arg = input[i++];
}
}(), // immediately invoked lambda/closure object
...); // C++17 fold expression
}

int main() {
int a = 1, b = 2, c = 3;
set_params({10, 20}, a, b, c);

std::cout
<< a << ' '
<< b << ' '
<< c << '\n';
}


Related Topics



Leave a reply



Submit