Home > Mobile >  Create a pointer(?) to a struct, residing in a <vector> c
Create a pointer(?) to a struct, residing in a <vector> c

Time:08-10

Currently, I access my vector of structs like so:

v_some_vector[some_position].name
v_some_vector[some_position].age
v_some_vector[some_position].etc

What I want to do:

s = v_some_vector[some_position];
s.name
s.age
s.etc

Any info on how I can do this is appreciated.

CodePudding user response:

C has two forms of indirection: Pointers and references. In the shown example, using reference is more appropriate. An example:

auto& s = v_some_vector[some_position];

CodePudding user response:

Alternatively, you could put whatever you want to do in a separate function

void foo(S s) {
  s.name...
  s.age
  s.etc
}

and call it with your vector's element:

foo(v_some_vector[some_position]);
  • Related