简体   繁体   English

意外的C字符串定义行为

[英]Unexpected C string definition behaviour

As far as I know the below code should not work. 据我所知,以下代码不起作用。 Yet, somehow this is OK on my compiler. 但是,以某种方式,这在我的编译器上是可以的。 Please could someone explain. 请有人解释。

int main()
{
    char *string;
    string = "Goo";
}

As far as I know the below code should not work 据我所知,以下代码不起作用

I'm afraid, your information is wrong. 恐怕您的信息有误。

char *string;
string = "Goo";

is perfectly valid. 是完全有效的。 This is basically, 这基本上是

  1. Define a char pointer string . 定义一个char指针string
  2. Put the base address of the string literal "Goo" into string . 字符串文字 "Goo"的基地址放入string

However, instead of being a char pointer, if string would have been an array, then this would not have been possible as array's cannot be assigned (except definition time though brace enclosed list). 然而,而不是作为一个char指针,如果string将是一个数组,那么这将是不可能的数组不能分配 (除定义时,虽然括号括起来的列表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM