Art*_*tie 5 c++ concatenation string-concatenation
Ps: This is more of a conceptual question.
I know this makes things more complicated for no good reason, but here is what I'm wondering. If I'm not mistaken, a const char* "like this" in c++ is pointing to l and will be automatically zero terminated on compile time. I believe it is creating a temporary variable const char* to hold it, unless it is keeping track of the offset using a byte variable (I didn't check the disassembly). My question is, how would you if even possible, add characters to this string without having to call functions or instantiating strings?
Example (This is wrong, just so you can visualize what I meant):
"Like thi" + 's';
Run Code Online (Sandbox Code Playgroud)
The closest thing I came up with was to store it to a const char* with enough spaces and change the other characters.
Example:
char str[9];
strcpy(str, "Like thi")
str[8] = 's';
Run Code Online (Sandbox Code Playgroud)
Clarification: Down vote: This question does not show any research effort; it is unclear or not useful
Ok, so the question has been highly down voted. There wasn't much reasoning on which of these my question was lacking on, so I'll try to improve all of those qualities.
My question was more so I could have a better understanding of what goes on when you simply create a string "like this" without storing the address of that string in a const char* I also wanted to know if it was possible to concatenate/change the content of that string without using functions like strcat() and without using the overloaded operator + from the class string. I'm aware this is not exactly useful for dealing with strings in C++, but I was curious whether or not there was a way besides the standard ways for doing so.
string example = "Like thi" + "s"; //I'm aware of the string class and its member functions
const char* example2 = "Like this"; //I'm also aware of C-type Strings (CString as well)
Run Code Online (Sandbox Code Playgroud)
It is also possible that not having English as my native language made things even worst, I apologize for the confusion.
您应该使用 C++ 库提供的字符串库,而不是使用普通的 char 字符串:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str = "Like thi";
cout << str << endl;
str = str + "s";
cout << str << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
通常,在 C 或 C++ 中不可能简单地连接纯char *字符串,因为它们只是指向字符数组的指针。如果您打算在自己的代码中进行任何字符串操作,那么几乎没有理由在 C++ 中使用裸字符数组。
即使您需要访问 C 表示形式(例如,对于外部库),您也可以使用string::c_str().
首先,没有任何东西null终止,而是以零终止。C 中的所有 char* 字符串都以'\0'.
当你在代码中做这样的事情时:
char *name="Daniel";
Run Code Online (Sandbox Code Playgroud)
编译器将生成一个包含以下内容的字符串:
Daniel\0
Run Code Online (Sandbox Code Playgroud)
并name根据变量上下文(成员,静态,...)在程序执行期间的某个时间初始化指针以指向它。
向 追加任何内容都name不会按您的预期工作,因为 by 指向的内存name是不可更改的,并且您可能会遇到访问冲突错误或覆盖其他内容。
拥有
const char* copyOfTheName = name;
Run Code Online (Sandbox Code Playgroud)
不会创建有问题的字符串的副本,它只会copyOfTheName指向原始字符串,所以有
copyOfTheName[6]='A';
Run Code Online (Sandbox Code Playgroud)
将完全一样
name[6]='A';
Run Code Online (Sandbox Code Playgroud)
只会给你带来麻烦。
std::strcat代替使用。请研究一下C 语言中基本字符串操作的工作原理。