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

Talk about things C/C++, some related to AutoHotkey
1100++
Posts: 78
Joined: 10 Feb 2018, 19:05

How to Access Members of a Vector from a Pointer

Post by 1100++ » 30 Dec 2019, 18:05

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;
}


1100++
Posts: 78
Joined: 10 Feb 2018, 19:05

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

Post by 1100++ » 30 Dec 2019, 19:21

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;
}

Post Reply

Return to “C/C++”