C++/String/string data
Версия от 14:21, 25 мая 2010; (обсуждение)
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
*/