Returning a pointer from function : function prototype « Function « C++ Tutorial






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

using namespace std;

string* ptrToElement(vector<string>* const pVec, int i);

int main()
{
    vector<string> i;
    i.push_back("A");
    i.push_back("B");
    i.push_back("C");

    cout << *(ptrToElement(&i, 0)) << "\n\n";

    string* pStr = ptrToElement(&i, 1);
    cout << *pStr << "\n\n";

    string str = *(ptrToElement(&i, 2));
    cout << str << "\n\n";

    *pStr = "test";
    cout << i[1] << endl;
    return 0;
}

string* ptrToElement(vector<string>* const pVec, int i)
{
    return &((*pVec)[i]);
}








7.2.function prototype
7.2.1.Use a function prototype to enforce strong type checking
7.2.2.Use a function's definition as its prototype, appears before main
7.2.3.Returning a pointer from function