Returning a pointer

I have a piece of code that im not 100% what it means.

 
string* ptrToElemet(Vector<string>* const pVec, int i)


Is this basically saying create a pointer called prtToElement that points to a string. I am slightly confused with the next part. So its saying create a vector but also a constant string pointer called pVec? and then create an interger called i?
that's a function
yeh its a function to return a pointer

The whole code is

1
2
3
4
5
 
string* ptrToElemet(Vector<string>* const pVec, int i)
{
return &((*pVec)[i];
}
Its a function header.

string* // the function will return a pointer to a string

ptrToElemet // this is simply the name of the function
1
2
Vector<string>* const pVec // first parameter of the function
                    // pVec is a const pointer to a vector of type string 

int i // second parameter of function: simply an int called 'i'
Last edited on
So ptrToElement is a function, not a pointer?

What id happening if i do string* pStr=ptrToElement(&inventory,1);

I get that it is creating a pointer called pStr, but is it pointing only to the address of inventory slot 1. I think the ptrToElement is confusing me
This is the code i am working from

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Inventory Pointer
// Demonstrates returning a pointer

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//returns a pointer to a string element
string* ptrToElement(vector<string>* const pVec, int i);

int main()
{
    vector<string> inventory;
    inventory.push_back("sword");
    inventory.push_back("armor");
    inventory.push_back("shield");

    //displays string object that the returned pointer points to
    cout << "Sending the object pointed to by returned pointer to cout:\n";       
    cout << *(ptrToElement(&inventory, 0)) << "\n\n";

    //assigns one pointer to another -- inexpensive assignment
    cout << "Assigning the returned pointer to another pointer.\n";
    string* pStr = ptrToElement(&inventory, 1);
    cout << "Sending the object pointed to by new pointer to cout:\n";
    cout << *pStr << "\n\n";
    
    //copies a string object -- expensive assignment
    cout << "Assigning object pointed to by pointer to a string object.\n";
    string str = *(ptrToElement(&inventory, 2));  
    cout << "Sending the new string object to cout:\n";
    cout << str << "\n\n";
    
    //altering the string object through a returned pointer
    cout << "Altering an object through a returned pointer.\n";
    *pStr = "Healing Potion";
    cout << "Sending the altered object to cout:\n";
    cout << inventory[1] << endl;

	int x; cin >> x;
    
    return 0;
}

string* ptrToElement(vector<string>* const pVec, int i)
{
    //returns address of the string in position i of vector that pVec points to
    return &((*pVec)[i]);  
}

// Inventory Pointer
// Demonstrates returning a pointer

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//returns a pointer to a string element
string* ptrToElement(vector<string>* const pVec, int i);

int main()
{
    vector<string> inventory;
    inventory.push_back("sword");
    inventory.push_back("armor");
    inventory.push_back("shield");

    //displays string object that the returned pointer points to
    cout << "Sending the object pointed to by returned pointer to cout:\n";       
    cout << *(ptrToElement(&inventory, 0)) << "\n\n";

    //assigns one pointer to another -- inexpensive assignment
    cout << "Assigning the returned pointer to another pointer.\n";
    string* pStr = ptrToElement(&inventory, 1);
    cout << "Sending the object pointed to by new pointer to cout:\n";
    cout << *pStr << "\n\n";
    
    //copies a string object -- expensive assignment
    cout << "Assigning object pointed to by pointer to a string object.\n";
    string str = *(ptrToElement(&inventory, 2));  
    cout << "Sending the new string object to cout:\n";
    cout << str << "\n\n";
    
    //altering the string object through a returned pointer
    cout << "Altering an object through a returned pointer.\n";
    *pStr = "Healing Potion";
    cout << "Sending the altered object to cout:\n";
    cout << inventory[1] << endl;

	int x; cin >> x;
    
    return 0;
}

string* ptrToElement(vector<string>* const pVec, int i)
{
    //returns address of the string in position i of vector that pVec points to
    return &((*pVec)[i]);  
}

You posted the code twice by accident, so I'll just repost it here:

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
58
59
// Inventory Pointer
// Demonstrates returning a pointer

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//returns a pointer to a string element
string* ptrToElement(vector<string>* const pVec, int i);

int main()
{
    vector<string> inventory;
    inventory.push_back("sword");
    inventory.push_back("armor");
    inventory.push_back("shield");

    //displays string object that the returned pointer points to
    cout << "Sending the object pointed to by returned pointer to cout:\n";       
    cout << *(ptrToElement(&inventory, 0)) << "\n\n";
    // the function call returns a pointer to a string pointing to element 
    // 0 of the inventory vector. This returned pointer is being dereferenced 
    // and will thus print out "sword". 

    //assigns one pointer to another -- inexpensive assignment
    cout << "Assigning the returned pointer to another pointer.\n";
    string* pStr = ptrToElement(&inventory, 1);
    // the function call returns a pointer. The pointer pStr is then assigned 
    // the return value of the function call. Simply put, pStr is now pointing 
    // to element 1 of the inventory vector
    cout << "Sending the object pointed to by new pointer to cout:\n";
    cout << *pStr << "\n\n"; 
    
    //copies a string object -- expensive assignment
    cout << "Assigning object pointed to by pointer to a string object.\n";
    string str = *(ptrToElement(&inventory, 2));  
    // the function returns a pointer, which is then dereferenced to give
    // us the string. This string is then assigned to 'str'
    cout << "Sending the new string object to cout:\n";
    cout << str << "\n\n";
    
    //altering the string object through a returned pointer
    cout << "Altering an object through a returned pointer.\n";
    *pStr = "Healing Potion";
    cout << "Sending the altered object to cout:\n";
    cout << inventory[1] << endl;

	int x; cin >> x;
    
    return 0;
}

string* ptrToElement(vector<string>* const pVec, int i)
{
    //returns address of the string in position i of vector that pVec points to
    return &((*pVec)[i]);  
}


I've included some (hopefully) helpful comments in the code below.
Ok I think I'm getting it now. Think the wording in the book is not the best.

One thing I am still confused about though is the string*

 
String* ptrToElement (vector <string>*const piece, into i)


I thought if you had string* something
It would make that "something" a pointer because of the *, but it doesn't in this case, I'm really confused how this is not being declared a pointer.

I have only started to learn c++ so confused easily XD
Well, take a look at the context.

In this context, the string* is a return type. It cannot possibly be a declaration of a pointer, as ptrToElement is followed by 2 parameters in parenthesis (We know there are parameters as they start with a data type).

Declaration and initialization of a string pointer would be like this: string* pStr = &str

If you still don't get it, think of it in terms of simpler code:
int a; // makes an int variable called 'a'

int a = num; // declares an int 'a' and initializes it with 'num'

1
2
int a(int num);
 // function header: return type is int, function name is 'a', and it takes a single int //parameter called 'num  
Last edited on
Ah OK I'm getting it now. Thanks a lot for all your help. It has really helped :)
Topic archived. No new replies allowed.