Can I use vector as pointer?

I'd like to do something similar

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
int create(int number, vector<string>* status)
{
    vector<string> status_list;

    if(number < 10) {
        status_list.push_back("small");
    } else if (number > 100) {
        status_list.push_back("big");
    }

    number -= 11;
    
    if(number < 0) {
        number *= -1;
        status_list.push_back("subtraction, it is negative");
    } else {
        status_list.push_back("subtraction, it is positive");
    }
    
    if((number % 2) == 0) {
        status_list.push_back("even number");
        number /= 2;
    }
    
    status = &status_list;
    
    return number;
}


In main:
1
2
3
4
5
vector<string> output = {};

int number = create(17, &output);

//do something 


The number is 3, but output is empty. Is it possible to get status_list in the main?
In C++ you really should prefer passing by reference instead of by pointer.

Something more like:

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
int create(int number, vector<string>& status_list)
{
    //vector<string> status_list; Not needed.
    // What is with all the magic numbers?
    if(number < 10) {
        status_list.push_back("small");
    } else if (number > 100) {
        status_list.push_back("big");
    }

    number -= 11;
    
    if(number < 0) {
        number *= -1;
        status_list.push_back("subtraction, it is negative");
    } else {
        status_list.push_back("subtraction, it is positive");
    }
    
    if((number % 2) == 0) {
        status_list.push_back("even number");
        number /= 2;
    }
    
   // status = &status_list;  // Not needed.
    
    return number;
}


Edit: and main()
1
2
3
4
vector<string> output; // No need of the "empty" initializer vectors are empty by default.

int number = create(17, output);
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
int byvalue( int a ) {
  a = 42; // will not affect the caller
}

int byref( int& a ) {
  a = 42; // modifies caller's variable
}

int byvalue( int* a ) {
  int b;
  *a = 42; // modifies caller's variable
  a = b; // will not affect the caller, a points to b
}

Therefore,
1
2
3
4
5
6
int create(int number, vector<string>* status)
{
    vector<string> status_list;
    *status = status_list;
    return 0;
}



[Edit]
The "use vector as pointer" sounds semantically wrong. Rather "have a pointer to vector".

The fun part is that (1) vector HAS-A pointer and (2) vector represents an array and plain arrays decay to pointers.

One more:
you don't need local vector if you can modify caller's vector via pointer:
1
2
3
4
5
6
7
8
9
10
11
12
13
int create( int number, vector<string>* status )
{
    if ( status ) { // pointer must be valid
        status->clear(); // start with empty vector

        if(number < 10) {
            status->push_back("small");
        } else if (number > 100) {
            status->push_back("big");
        }
    }
    return number;
}

But as said pass by reference is better.
Last edited on
jlb - It's works for me, than you for answer.
Topic archived. No new replies allowed.