How to Access Members of a Vector from a Pointer

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: How to Access Members of a Vector from a Pointer

Re: How to Access Members of a Vector from a Pointer

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

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


Top