char *和char []之间的区别
收藏

我知道这是一个非常基本的问题。我对以下内容为何以及如何有所不同感到困惑。

char str[] = "Test";
char *str = "Test";

最佳答案

char str[] = "Test";

Is an array of chars, initialized with the contents from "Test", while

char *str = "Test";

是指向文字(常量)字符串“ Test”的指针。

The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of "Test", while the pointer simply refers to the contents of the string (which in this case is immutable).

    公众号
    关注公众号订阅更多技术干货!