char str[20]; > str="HELLO"; 不是。为什么? 最佳答案 char str[20]="HELLO"; 上面是一个-6ren">
gpt4 book ai didi

c - 读取没有函数的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 07:53:09 24 4
gpt4 key购买 nike

>char str[20]="HELLO"; 

是正确的,而

>char str[20];
>
str="HELLO";

不是。为什么?

最佳答案

char str[20]="HELLO";

上面是一个声明,也是标识符str的一个定义,并且有一个初始化器“你好”。 C 标准对这个初始化说:

An initializer specifies the initial value stored in an object.

An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.

char str[20];
str="HELLO";

这里的第一行又是一个声明,也是标识符 str定义,但没有初始化程序。第二行是带有赋值 表达式的语句。 分配约束是:

An assignment operator shall have a modifiable lvalue as its left operand.

关于左值、数组……:

A modifiable lvalue is an lvalue that does not have array type, …

因此,尝试分配给数组是违反约束的。

关于c - 读取没有函数的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52508985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com