简体   繁体   中英

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.

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 (ie 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 .

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/

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;

}

实际的错误是您返回对将销毁范围的东西的引用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM