I'm trying to return a pointer, that points to an array of strings, from a function and then access elements in the array that the returned pointer points to. I seem to get a pointer back from the function, but when I try to output individual elements of the array to the computer screen, my program seems to just stop outputting and end the program.
Running the code below, the pointer to the array outputs as expected (see screen output screenshot). However, when I try to access the array's elements, none of the elements are output to the screen and the program just ends. Any ideas/thoughts would be greatly appreciated. FYI, I realize that I'm not using the data that's being passed to the function, but that's because I'm trying to figure out how to access the data that gets returned first.
roster.h
#ifndef ROSTER_H
#define ROSTER_H
#include <iostream>
#include <string>
class Roster {
public:
std::string* parseData(const std::string Data);
};
#endif
roster.cpp
#include <iostream>
#include <string>
#include "roster.h"
std::string* Roster::parseData(const std::string Data) {
std::string stringData[3] = { "abc", "def", "ghi" };
return stringData;
}
rostertest.cpp
#include <iostream>
#include <string>
#include "roster.h"
void rosterClassTest()
{
Roster roster;
std::string* returnData = roster.parseData("red,blue,black,gray");
std::cout << "pointer returned: "
<< returnData << std::endl << std::endl;
std::cout << "element 0 of array: "
<< returnData[0] << std::endl << std::endl;
std::cout << "element 1 of array: "
<< returnData[1] << std::endl << std::endl;
std::cout << "element 2 of array: "
<< returnData[2] << std::endl << std::endl;
}