Hi,
I have written the following program but its not doing what's it supposed to do ! For some reason sometimes nothing or rubbish is being printed onto the screen. However, if I don't use function but instead do everything within main(), then its fine. What's the problem with the prgram ?
#include <iostream>
#include <string>
using namespace std;
const char* getArray(string s){
const char* temp = s.c_str();
return temp;
}
int main(){
string s = "STRANGE";
const char* temp = getArray(s);
int i = 0;
while(temp[i] != '\0'){
cout<<temp[i]<<" ";
i++;
}
cout<<endl;
return 0;
}
Thanks