Hand a struct member to a function

Howdy all,

I’m fairly new to c++, so I hope this isn’t a stupid question. Apologies in advance if that’s the case!

I have a struct that looks something like this :

struct config_t
  unsigned int probe1Value;
  unsigned int probe2Value;  

I then use the struct in another function like this :

void myFunction(int probe, int value)
    configuration.probe1Value = value;
  else if(probe==2)
    configuration.probe2Value = value;

Is it possible to hand the struct member in so I don’t have to use the ugly if loop in the function? This is a stripped down version - there are many more values in the full deal, so it’s even uglier.

I’m hoping to end up with the function looking something like this :

void myFunction(<whateverthetypeis?> structMember, int value)
   structMember   = value;

Any thoughts or different approaches are more than welcomed.



You can pass a pointer (struct config_t *p) to the structure or pass by reference (struct config_t &p), which does not make a copy of the data and allows the function to modify the original data.

You can pass the data as a structure (struct config_t), in which case a copy of the data is created locally for the function.

I would define a type for this structure (typedef), which would make usage a bit less clunky and more obvious.

If you want to modify one element of a struct, without passing the whole struct, do that.

void myFunction(int &probe, int value)
   probe = value;
   myFunction(configuration.probe1Value, value);

However, unless this is a contrived example, using a function that does nothing but assign one value to one variable, unconditionally, is a waste of effort.