意外的 C 字符串定义行为
Unexpected C string definition behaviour
据我所知,下面的代码应该不起作用。然而,不知何故,这在我的编译器上是可以的。请哪位大侠解释一下。
int main()
{
char *string;
string = "Goo";
}
As far as I know the below code should not work
恐怕你的信息有误。
char *string;
string = "Goo";
完全有效。这基本上是,
- 定义一个
char
指针string
.
- 将字符串字面量
"Goo"
的基地址放入string
.
但是,如果 string
不是一个 char
指针,而是一个数组,那么这是不可能的,因为数组不能 分配 (定义时间除外,但括号括起来的列表)。
据我所知,下面的代码应该不起作用。然而,不知何故,这在我的编译器上是可以的。请哪位大侠解释一下。
int main()
{
char *string;
string = "Goo";
}
As far as I know the below code should not work
恐怕你的信息有误。
char *string;
string = "Goo";
完全有效。这基本上是,
- 定义一个
char
指针string
. - 将字符串字面量
"Goo"
的基地址放入string
.
但是,如果 string
不是一个 char
指针,而是一个数组,那么这是不可能的,因为数组不能 分配 (定义时间除外,但括号括起来的列表)。