C++ 使用指针将一个字符数组添加到另一个字符数组

C++ 使用指针将一个字符数组添加到另一个字符数组,c++,pointers,C++,Pointers,我有这样一个功能,可以正常工作: char* add(char* origText, char* paste) { char* pointerToOrigText = origText; while (*pointerToOrigText!='\0') pointerToOrigText++; while (*paste!='\0') *pointerToOrigText++=*paste++; *pointerToOrigText

我有这样一个功能,可以正常工作:

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”

因此,我将两个字符串合并为一个字符串。但是当我使用这个函数时:

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?

*pointerToNewText++*helpPointer++

*pointerToNewText++*粘贴++//错了。只增加了指针指向的值。

原始文本=新文本//无用。

您应使用以下代码:

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 ;
}

在代码中,字符串的地址通过复制到本地变量
origText
的值传递给函数。行
origText=newText不会更改外部的实际指针。必须将指针传递给原始指针

char* add (char** origText,char *paste)
*origText = newText;
或者使用“通过引用传递”

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

此外,您的代码不会返回任何内容。您必须返回一些
char*
或更改签名以返回函数中的
void

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.
为什么在函数中分配内存会影响指针origText


您的函数版本只是将额外的文本添加到原始字符串上,覆盖原始字符串之后存储在内存中的内容。如果没有什么重要的东西,或者它可能会导致崩溃或造成安全问题,这可能会正常工作。正确的解决方案是分配一个足够大的新内存块来容纳新的组合字符串,并将两个字符串复制到那里。

我希望origText中有足够的内存,否则会损坏内存。请使用
std::string
。您可以使用它在一行中编写整个函数定义。如果你的书推荐了你的写作方式,那就另选一本好书。@Mahesh+1这本书在很多方面似乎都是错的levels@Mahesh我不明白为什么C++书籍(除了少数例外)是真正的C类图书而不是Stutt。C++有30年,而现在人们还不明白C++是不是C。人们认为模板只是<代码>容器< /代码>,在任何地方都可以使用原始指针和<代码>新/删除>代码,并且做低级的C风格技巧,认为他们正在优化代码……当使用C字符串时,使用<代码> STD::StCAT。
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 origText = "abc";
std::string paste = "def";
std::string newText = origText + paste; // Does exactly what you think it does.