user4557113
user4557113

Reputation:

Pointers and references in C++

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.

Upvotes: 1

Views: 89

Answers (4)

Arun A S
Arun A S

Reputation: 7006

This will fix your errors

#include <iostream>
#include <cstdlib>
#include <string>

std::string string_reverse(std::string& str){   // made change here
  std::string rev = "";
  for(int i= str.length() -1; i >=0; i--){
    rev+=str[i];
  }
  return rev;        // made change here

}

int main(){
  std::string s="";
  std::cout<<"Please enter a string..."<<std::endl;
  std::cin>>s;
  std::cout<< string_reverse(s)<<std::endl;

}

The problem with your code, as suggested by others is that the variable rev will be destroyed after the function returns ( It becomes out of scope ), and your code causes Undefined Behaviour.

Here are some links you might want to read ( These links can provide you with more information than I can, but you must have patience to read all of it ).

http://www.cs.fsu.edu/~myers/c++/notes/references.html

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

http://www.tutorialspoint.com/cplusplus/cpp_function_call_by_reference.htm

http://www.cplusplus.com/articles/z6vU7k9E/

Upvotes: 0

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

First of all, you don't return a reference for scope local variables

std::string& string_reverse(std::string& str){
        // ^

change that to

std::string string_reverse(std::string& str){

A reference can't be returned here, because the rev variable will be destroyed after the function returns (see also Can a local variable's memory be accessed outside its scope?).

Secondly you don't use & to create a reference:

 return &rev;
     // ^

That will take the address of rev (i.e. a pointer). You simply write (for either case)

 return rev;

Last but not least you don't need to pass a non const reference for the input parameter. It's better to write

std::string string_reverse(const std::string& str){ 
                        // ^^^^^

if the function doesn't change str.

Upvotes: 2

Xxxo
Xxxo

Reputation: 1931

The actual error is that you return a reference to something that will be destroyed out of scope.

Upvotes: 0

Paul Evans
Paul Evans

Reputation: 27567

You're mixing references and addressses, get rid of both and it'll work:

std::string string_reverse(std::string& str){
    std::string rev = "";
    for(int i= str.length() -1; i >=0; i--){
        rev+=str[i];
    }
    return rev;

}

Upvotes: 0

Related Questions