I'm fairly new to C++ and thus trying to understand exactly how pointers and references work. Thus I've a simple program that should reference a string ans return a reference to another string. I don't want to copy either. Here's the code
#include <iostream>
#include <cstdlib>
#include <string>
std::string& string_reverse(std::string& str){
std::string rev = "";
for(int i= str.length() -1; i >=0; i--){
rev+=str[i];
}
return &rev;
}
int main(){
std::string s="";
std::cout<<"Please enter a string..."<<std::endl;
std::cin>>s;
std::cout<< string_reverse(s)<<std::endl;
}
However my code throws up a lot of errors. It would really help if someone can elaborate on the different ways this can be done and the underlying reason for each and which is the right way.