C++ 如何复制或连接两个字符*
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10609326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to copy or concatenate two char*
提问by mister
How do you concatenate or copy char* together?
你如何连接或复制 char* 在一起?
char* totalLine;
const char* line1 = "hello";
const char* line2 = "world";
strcpy(totalLine,line1);
strcat(totalLine,line2);
This code produces an error!
此代码产生错误!
segmentation fault
I would guess that i would need to allocate memory to totalLine?
我猜我需要为 totalLine 分配内存吗?
Another question is that does the following copy memory or copy data?
另一个问题是,下面是复制内存还是复制数据?
char* totalLine;
const char* line1 = "hello";
totalLine = line1;
Thanks in advance! :)
提前致谢!:)
回答by Oliver Charlesworth
I would guess that i would need to allocate memory to totalLine?
我猜我需要为 totalLine 分配内存吗?
Yes, you guessed correctly. totalLine
is an uninitialized pointer, so those strcpy
calls are attempting to write to somewhere random in memory.
是的,你猜对了。 totalLine
是一个未初始化的指针,因此这些strcpy
调用试图写入内存中的某个随机位置。
Luckily, as you've tagged this C++, you don't need to bother with all that. Simply do this:
幸运的是,因为您已经标记了这个 C++,所以您不需要为所有这些而烦恼。只需这样做:
#include <string>
std::string line1 = "hello";
std::string line2 = "world";
std::string totalLine = line1 + line2;
No memory management required.
无需内存管理。
does the following copy memory or copy data?
下面是复制内存还是复制数据?
I think you mean "is the underlying string copied, or just the pointer?". If so, then just the pointer.
我想你的意思是“是底层的字符串被复制了,还是只是指针?”。如果是这样,那么只是指针。
回答by zwol
Yes, you need to allocate memory to totalLine
. This is one way to do it; it happens to be my recommended way to do it, but there are many other ways which are just as good.
是的,您需要将内存分配给totalLine
. 这是一种方法;这恰好是我推荐的方法,但还有许多其他方法也同样好。
const char *line1 = "hello";
const char *line2 = "world";
size_t len1 = strlen(line1);
size_t len2 = strlen(line2);
char *totalLine = malloc(len1 + len2 + 1);
if (!totalLine) abort();
memcpy(totalLine, line1, len1);
memcpy(totalLine + len1, line2, len2);
totalLine[len1 + len2] = 'const char* Line1{ "Hallo" };
const char* Line2{ "World" };
char* TotalLine{ new char[strlen(Line1) + strlen(Line2) + 1] };
TotalLine = strcpy(TotalLine, Line1);
TotalLine = strcat(TotalLine, Line2);
';
[EDIT:I wrote this answer assuming this was a C question. In C++, as Oli recommends, just use std::string
. ]
[编辑:我写这个答案时假设这是一个 C 问题。在 C++ 中,正如 Oli 建议的那样,只需使用std::string
. ]
回答by GDevYoussef
totalLine
has a garbage value
totalLine
有垃圾价值
#include <iostream>
#include <cstring>
using namespace std;
int main() {
const char* Line1 ="aaa";
const char* Line2 ="bbb";
char* TotalLine{ new char[strlen(Line1) + strlen(Line2) + 1] };
TotalLine = strcpy(TotalLine, Line1);
TotalLine = strcat(TotalLine, Line2);
cout << TotalLine <<endl;
}
Note=> If you work on Visual Studio you need
#define _CRT_SECURE_NO_WARNINGS
注意=> 如果你在 Visual Studio 上工作,你需要
#define _CRT_SECURE_NO_WARNINGS
回答by Yang
Concentrate "aaa" and "bbb" to "aaabbb":
将“aaa”和“bbb”浓缩为“aaabbb”:
##代码##