String in function parameter

后端 未结 3 1007
时光说笑
时光说笑 2020-12-15 08:41
int main()
{
        char *x = \"HelloWorld\";
        char y[] = \"HelloWorld\";

        x[0] = \'Z\';
        //y[0] = \'M\';

        return 0;
}
3条回答
  •  臣服心动
    2020-12-15 08:59

    function("MyString");
    

    is similar to

    char *s = "MyString";
    function(s);
    

    "MyString" is in both cases a string literal and in both cases the string is unmodifiable.

    function("MyString");
    

    passes the address of a string literal to function as an argument.

提交回复
热议问题