2

私は正しく動作するこのような機能を持っています:

char* add(char* origText, char* paste)
{
    char* pointerToOrigText = origText;
    while (*pointerToOrigText!='\0')
        pointerToOrigText++;
    while (*paste!='\0')
        *pointerToOrigText++=*paste++;
    *pointerToOrigText='\0';
 }

例:origText = "abc", paste = "def"
関数の後:origText = "abcdef", paste="def"

そこで、2 つの文字列を 1 つにマージしました。しかし、この関数を使用すると:

char* add (char* origText, char *paste)
{
    int newLength = strlen(origText) + strlen(paste)+ 1; // + '\0'
    char* newText = new char[newLength]; // we want to make sure that 2 strings will fit.

    char* pointerToNewText = newText; // pointer to char array where we will merge strings
    char* helpPointer = origText; // helps us count until '\0'

    while (*helpPointer!='\0')
    {
        *pointerToNewText=*helpPointer;
        *pointerToNewText++; *helpPointer++;
    }

    while (*paste!='\0')
    {
        *pointerToNewText=*paste;
        *pointerToNewText++; *paste++;
    }

    *pointerToNewText='\0';

    origText = newText;
    
   // cout <<origText<<endl;
}

外部関数の出力は次のとおりです。

  • origText = "abc"、paste = "def"
  • 関数の後: origText = "abc", paste = "def"

私の本では、それは次の行によるものだと説明しています。

char* newText = new char[newLength]; 

しかし、私はそれを取得しません。関数にメモリを割り当てるとポインタorigTextに影響するのはなぜですか?

4

4 に答える 4

4

関数内でorigTextは、渡されたものとは別の変数です。したがって、そこで何をしても ( などorigText = newText) 、呼び出し元の変数には影響しません。

代わりに、関数は新しい文字列へのポインターを返すようになっているように見えます。

char* // That's the function's return type: it must return that
add(const char* origText, // Added const: the function doesn't change this string
    const char* paste)    // And again
{
    // Your code (with a bit more const), followed by
    return newText;
}

これで、関数を呼び出すときに、その戻り値を使用できます。

const char* origText = "abc";
const char* paste = "def";

char* newText = add(origText, paste);

std::cout << origText << std::endl;    // abc - unchanged
std::cout << paste    << std::endl;    // def - unchanged
std::cout << newText  << std::endl;    // abcdef - result of concatenation

delete [] newText; // Don't forget to delete whatever you create with new.

この厄介なメモリ管理がどのように機能するかを理解したら、std::stringクラスを使用してすべてを行う方法を学ぶ必要があります。

std::string origText = "abc";
std::string paste = "def";
std::string newText = origText + paste; // Does exactly what you think it does.
于 2013-08-03T15:25:12.217 に答える
1

*pointerToNewText++; *helpPointer++;

*pointerToNewText++; *貼り付け++; //間違っています。ポインタが指す値だけが増えました。

origText = newText; //役に立たない。

以下のコードを使用する必要があります。

char* add (char* origText,char *paste)
{
    int newLength = strlen(origText) + strlen(paste)+ 1; // + '\0'
    char* newText = new char[newLength]; // we want to make sure that 2 strings will fit.

    char* pointerToNewText = newText; // pointer to char array where we will merge strings
    char* helpPointer = origText; // helps us count until '\0'

    while (*helpPointer!='\0')
    {
        *pointerToNewText=*helpPointer;
        pointerToNewText++; helpPointer++;
    }

    while (*paste!='\0')
    {
        *pointerToNewText=*paste;
        pointerToNewText++; paste++;
    }

    *pointerToNewText='\0';

    return newText ;
}
于 2013-08-03T15:22:45.487 に答える
1

origTextあなたのコードでは、文字列のアドレスは、ローカル変数にコピーすることにより、値によって関数に渡されます。行origText = newText;は、外部の実際のポインターを変更しません。ポインターを元のポインターに渡す必要があります

char* add (char** origText,char *paste)
*origText = newText;

または参照渡しを使用する

char* add (char*& origText,char *paste)
origText = newText;

さらに、あなたのコードは何も返しません。一部を返却するchar*か、署名を変更して返却する必要がありますvoid

于 2013-08-03T15:25:06.693 に答える
0

関数でのメモリの割り当てがポインタ origText に影響するのはなぜですか。

関数のバージョンは、元の文字列に追加のテキストを追加するだけで、元の文字列の後にメモリに保存されていたものを上書きします。重要なものが何もない場合は、問題なく動作する可能性があります。さもないと、クラッシュやセキュリティ上の問題が発生する可能性があります。適切な解決策は、新しい結合文字列を保持するのに十分な大きさの新しいメモリを割り当て、そこに両方の​​文字列をコピーすることです。

于 2013-08-03T15:31:33.310 に答える