pointers to vectors

hey guys am trying out something new but i need some srs help i have two code below the first compiles and run bt de second also runs bt crashes...so my question is y is different when i put my vector as an argument as shown in first code and when i just declare it within my function in second code; thanx in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 #include <iostream>
#include <vector>

using namespace std;

void  cube(vector<string> *numbers)
{


    numbers->push_back("5");
    numbers->push_back("4");
    numbers->push_back("hsbfjbfsjbfjbjwbdfwjhdfw");
    numbers->push_back("2");
    numbers->push_back("1");

    cout<<numbers->at(2);
}
int main()
{
    
    vector<string>num;
    cube(&num);

    return 0;
} 


second code 


#include <iostream>
#include <vector>

using namespace std;

void  cube()
{
vector<string> *numbers;

    numbers->push_back("5");
    numbers->push_back("4");
    numbers->push_back("hsbfjbfsjbfjbjwbdfwjhdfw");
    numbers->push_back("2");
    numbers->push_back("1");

    cout<<numbers->at(2);
}
int main()
{

   // vector<string>num;
    cube();

    return 0;
}

you have a "real" vector instantiated in the first example (line) 21 and you pass a reference to this into your function.
In second example you don't. You just have a pointer to a vector. You'd have to new one up.
Last edited on
thanx for de reply but hw would i initiate dat in my second code...i have seen "new" bt dont knw how to use it
take a look here:
http://stackoverflow.com/questions/6946217/pointer-to-a-vector

at the answer that's had 27 up-votes. The important thing to realise is that this:
vector<string> *numbers;
is not really creating a vector.
Last edited on
thanx i got it
Topic archived. No new replies allowed.