Home > Back-end >  c vector pointer reference issue
c vector pointer reference issue

Time:06-29

so I am having some issues with creating and using pointers for vectors. The problem I'm trying to solve with these pointers, is referencing data, without having an excess amount of code. This is how I'm currently defining the variables:

// Data vectors
std::vector<int16_t> amountData;
std::vector<float> speedData;

std::vector<int16_t> *pointerr = &amountData; // Should be auto, just testing

I reference the data used multiple times through the code, which is why it would be easier if I could just have a pointer for the active data (data which I intend to use). I can't get it to work though, with using commands such as "*pointerr.size();" and such. I get the error:

request for member 'size' in 'pointerr', which is of pointer type 'std::vector<short int>*' (maybe you meant to use '->' ?)

and when using '*pointerr->size();', I get:

invalid type argument of unary '*' (have 'std::vector<short int>::size_type {aka long long unsigned int}')

I know its probably just me not fully understanding pointers/vectors, and that I'm probably missing something. Most of the other simallar questions don't really answer my problem (as far as I understand). I appreciate any sort of help/ideas and such, thanks in advance:)

CodePudding user response:

You want pointerr->size() (without a *); the -> operator does the dereference of pointerr for you.

Or alternatively, (*pointerr).size() which is equivalent. Your attempt of *pointerr.size() was close, but the . operator has higher precedence than *, and you have to derefence the pointer before you can apply . to the object it points to.

  • Related