Page 1 of 1

How to Access Members of a Vector from a Pointer

Posted: 30 Dec 2019, 18:05
by 1100++
I'm trying to write code where I point to a vector rather than directly include it in a struct. How would I access the elements of the vector pointed to in this way? Here's some code to give an idea of what I'm trying to do.

Code: Select all

#include <iostream>
#include <vector>

typedef struct {
    __int64 size;
    vector<__int64>* numbers;
} num_array;

int main() {
    num_array array;

    // I know how to do this.
    array.numbers->resize(5);

    // I don't know how to do this.  These both return errors.
    array.numbers[0] = 5;
    *(array.numbers)[0] = 5;
    cout << array.numbers[0];
    return 0;
}


Re: How to Access Members of a Vector from a Pointer  Topic is solved

Posted: 30 Dec 2019, 19:21
by 1100++
I figured out how to do it.

Code: Select all

#include <iostream>
#include <vector>

typedef struct {
    __int64 size;
    vector<__int64>* numbers;
} num_array;

int main() {
    num_array array;

    array.numbers->resize(5);

    // This is how you access a vector from a pointer.
   (*array.numbers)[0] = 5;
    cout << (*array.numbers)[0];
    return 0;
}