字符串语句

Ven*_*nus 2 c c++

在C++中谈论字符串时,以下语句之间有什么区别:s1=s2strcpy (s1, s2)?假设s1s2(原始版本:类型' char',已修改)类型char *.

Jon*_*ler 16

鉴于:

char s1, s2;
...
s1 = s2;          // Assigns value in s2 to s1
strcpy(s1, s2);   // Error detected by compiler; strcpy() takes char pointers
Run Code Online (Sandbox Code Playgroud)

鉴于:

char *s1, *s2;
...
s1 = s2;         // s1 points to the same 'string' that s2 does
strcpy(s1, s2);  // the space pointed to by s1 contains the same
                 // characters as the space pointed to by s2.
Run Code Online (Sandbox Code Playgroud)

在第二种情况(指针)中,有许多警告要确保为实际字符串分配足够的空间 - 而不是指针.三点表示要确保事情初始化的工作.