c 字符串基础知识,为什么未分配?
我正在尝试学习基础知识,我认为声明 char[] 并为其分配一个字符串会起作用。 谢谢
int size = 100;
char str[size];
str = "\x80\xbb\x00\xcd";
给出错误“赋值中的类型不兼容”。怎么了? 谢谢
I am trying to learn the basics, I would think that declaring a char[] and assigning a string to it would work.
thanks
int size = 100;
char str[size];
str = "\x80\xbb\x00\xcd";
gives error "incompatible types in assignment". what's wrong?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用字符串文字初始化一个字符数组,但不能分配一个字符数组(与分配任何其他数组一样)。 OTOH,您可以分配一个指针,因此允许以下操作:
You can use a string literal to initialize an array of char, but you can't assign an array of char (any more than you can assign any other array). OTOH, you can assign a pointer, so the following would be allowed:
这实际上是学习编程语言最困难的部分之一...... str 是一个数组,即被保留并标记为 str 的内存的一部分(大小乘以字符,因此大小字符)。 str[0] 是第一个字符,str[1] 是第二个... str[size-1] 是最后一个字符。 str 本身,没有指定任何字符,是一个指向您执行操作时创建的内存区域的指针
正如 Jerry 明确指出的那样,在 C 中,您不能以这种方式初始化数组。您需要从一个数组复制到另一个数组,因此您可以执行以下操作
总结:区分指针、内存区域和数组非常重要。
祝你好运 - 我很确定您将在比您想象的更短的时间内掌握这些概念:)
This is actually one of the most difficult parts of learning a programming language.... str is an array, that is, a part of memory (size times a char, so size chars) that has been reserved and labeled as str. str[0] is the first character, str[1] the second... str[size-1] is the last one. str itself, without specifiying any character, is a pointer to the memory zone that was created when you did
As Jerry so clearly said, in C you can not initialize arrays that way. You need to copy from one array to other, so you can do something like this
Summarizing: It's very important to make a difference between pointers, memory areas and array.
Good luck - I am pretty sure that in less time than you imagine you will be mastering these concepts :)
char 数组在用作右值时可以隐式转换为 char*,但用作左值时则不能 - 这就是赋值不起作用的原因。
A char-array can be implicitely cast to a char* when used as Rvalue, but not when used as Lvalue - that's why the assignment won't work.
您不能使用
=
运算符分配数组内容。这只是 C 语言设计的一个事实。您可以在声明中初始化数组,但这与赋值是不同的操作。
请注意,在这种情况下,额外的“\0”将添加到字符串末尾。“不兼容类型”警告来自于语言处理数组表达式的方式。首先,字符串文字存储为具有静态范围的 char 数组(意味着它们在程序的整个生命周期中都存在)。因此,字符串文字“\x80\xbb\x00\xcd”的类型是“
45-element array ofchar
”。然而,在大多数情况下,数组类型的表达式将隐式地从类型“T 的 N 元素数组”转换(“衰减”)为“指向 T 的指针”,并且表达式的值将是第一个的地址。数组中的元素。因此,当您编写该语句时,文字的类型已从“
45-element array ofchar
”隐式转换为“指向char
的指针” >”,但赋值的目标是类型“100-element array ofchar
”,并且这些类型不兼容(除此之外,数组表达式不能是char
的目标代码>=运算符)。要将一个数组的内容复制到另一个数组,您必须使用库函数,例如
memcpy
、memmove
、strcpy
等。此外,对于strcpy
要正常运行,源字符串必须以 0 结尾。编辑根据下面 R 的评论,我删除了答案中更愚蠢的部分。
You cannot assign array contents using the
=
operator. That's just a fact of the C language design. You can initialize an array in the declaration, such asbut that's a different operation from an assignment.
And note that in this case, and extra '\0' will be added to the end of the string.The "incompatible types" warning comes from how array expressions are treated by the language. First of all, string literals are stored as arrays of
char
with static extent (meaning they exist over the lifetime of the program). So the type of the string literal "\x80\xbb\x00\xcd" is "45-element array ofchar
". However, in most circumstances, an expression of array type will implicitly be converted ("decay") from type "N-element array of T" to "pointer to T", and the value of the expression will be the address of the first element in the array. So, when you wrote the statementthe type of the literal was implicitly converted from "
45-element array ofchar
" to "pointer tochar
", but the target of the assignment is type "100-element array ofchar
", and the types are not compatible (above and beyond the fact that an array expression cannot be the target of the=
operator).To copy the contents of one array to another you would have to use a library function like
memcpy
,memmove
,strcpy
, etc. Also, forstrcpy
to function properly, the source string must be 0-terminated.Edit per R's comment below, I've struck out the more dumbass sections of my answer.
要将字符串文字分配给
str
数组,您可以使用字符串复制函数strcpy
。To assign a String Literal to the
str
Array you can use a the String copy functionstrcpy
.char a[100] = "\x80\xbb\x00\xcd";
或char a[] = "\x80\xbb\x00\xcd";
char a[100] = "\x80\xbb\x00\xcd";
ORchar a[] = "\x80\xbb\x00\xcd";
str 是数组的名称。数组的名称是第 0 个元素的地址。因此,str是一个指针常量。您无法更改指针常量的值,就像您无法更改常量一样(例如,您不能执行 6 = 5)。
str is the name of an array. The name of an array is the address of the 0th element. Therefore, str is a pointer constant. You cannot change the value of a pointer constant, just like you cannot change a constant (you can't do 6 = 5, for example).