C++/String/string data


Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из C\C++ эксперт
Перейти к: навигация, поиск

Passing Arguments by Value

   
#include <iostream>
#include <string>
using namespace std;
void printMessage(string);  
int main ()
{
   string str;
   cout << "Enter a string: ";
   cin >> str;
   printMessage(str); 
   return 0;
}
void printMessage (string s)
{
   cout << "You inputted " << s;
}


Use char pointer to point to the char array returned from string.data()

  
 
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
   string string1( "STRINGS" );
   const char *ptr1 = 0;
   ptr1 = string1.data();
   for ( int i = 0; i < string1.length(); i++ )
      cout << *( ptr1 + i ); // use pointer arithmetic
   return 0;
}
/* 
STRINGS
 */