Returning a Pointer from a String Involves Using an Asterisk in the Return Type - C++ Function

C++ examples for Function:Function Return

Description

Returning a Pointer from a String Involves Using an Asterisk in the Return Type

Demo Code

#include <iostream> 
#include <sstream> 
#include <stdlib.h> 

using namespace std; 

string *readCode() /*w w w  . j a v  a  2  s .c o m*/
{ 
  string *code = new string; 
  code->append("asdf"); 

  int randomnumber = rand(); 
  ostringstream converter; 
  converter << randomnumber; 

  code->append(converter.str()); 
  code->append(" asdf"); 

  return code; 
} 

int main() 
{ 
  string *newcode; 
  int index; 

  for (index = 0; index < 10; index++) 
  { 
    newcode = readCode(); 
    cout << *newcode << endl; 
  } 

  return 0; 
}

Result


Related Tutorials