Answer the question
In order to leave comments, you need to log in
How to get the first element of an array if there is a pointer to the array(wstring)?
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
wstring* splitParamsFromPayload(wstring payload) {
wstring arr[2] = { L"", L"" };
if (payload.find(L" ") != wstring::npos) {
wstringstream ws(payload);
ws >> arr[0];
ws.clear();
arr[1] = payload.erase(0, arr[0].length() + 1);
return arr;
}
else {
arr[0] = payload;
wcout << arr[0] << endl;
return arr;
}
}
int main() {
wstring* arr = splitParamsFromPayload(L"test");
wcout << (*arr)[0] << endl;
_wsystem(L"pause");
return 0;
}
Answer the question
In order to leave comments, you need to log in
If there is a pointer to the first element of the array of strings wstring* arr
, then *arr
— the first string in it; (*arr)[0]
is the first character of the first line. Everything is right here.
And the program does not work that's why. There is no way to return a raw array from a C++ function. (In this case, a pointer to an already deleted array is returned!) Solution: do not use raw arrays, but create, fill and return vector<wstring>
. In this case, the vector is suitable, because A line can have a different number of words.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question