私はC++ Primer Plusを読んできました。第8章の最初の演習では、文字列を出力する関数にアドレス経由で文字列を渡す必要があります。次に、別の関数は、2番目の引数(int型)が0に等しくない限り、関数が呼び出された回数に等しい回数だけ文字列を出力します。実際の文字列を出力する方法はわかりません。住所・アドレス。逆参照演算子を試しましたが、その結果エラーになります。文字列オブジェクトのアドレスを関数に渡す
/* Write a function that normally takes 1 argument, the address of a string, and prints that string once.
However, if a second, type int, argument is provided and is nonzero, the function should print the
string a number of times equal to the number of times that function has been called at that point.
(the number of times the function has been called is not equal to the int argument)
*/
#include <iostream>
#include <string.h>
using namespace std;
//global variables
int times_called = 0;
//function prototypes
void print_str(const string * str);
void print_str(const string * str, int i);
int main()
{
string str = "Gotta catch'em all!";
string * pstr = &str;
print_str(pstr);
print_str(pstr);
print_str(pstr, 1);
print_str(pstr, 0);
system("PAUSE");
}
void print_str(const string * str)
{
cout << str;
cout << endl;
}
void print_str(const string * str, int i)
{
if (i != 0)
{
for (int count = 0; count <= times_called; count++)
{
cout << str;
cout << endl;
}
}
else
{
cout << str;
cout << endl;
}
}
そしてまさにあなたの質問がある私は、彼らがこれらの行(code on ideone.com)に沿って何かを意味、と思いますか? – CoffeeandCode
times_calledは常にゼロになっていますか? print_str(...、1)とprint_str(...、0)の間に違いはありません –
その質問を含めるのを忘れました。文字列のアドレスではなく、文字列を出力する関数を取得するにはどうすればよいですか。 –