c 字符串基础知识,为什么未分配?

发布于 2024-09-25 15:14:58 字数 167 浏览 1 评论 0原文

我正在尝试学习基础知识,我认为声明 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

酒几许 2024-10-02 15:14:58

您可以使用字符串文字初始化一个字符数组,但不能分配一个字符数组(与分配任何其他数组一样)。 OTOH,您可以分配一个指针,因此允许以下操作:

char *str;

str = "\x80\xbb\x00\xcd";

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:

char *str;

str = "\x80\xbb\x00\xcd";
熟人话多 2024-10-02 15:14:58

这实际上是学习编程语言最困难的部分之一...... str 是一个数组,即被保留并标记为 str 的内存的一部分(大小乘以字符,因此大小字符)。 str[0] 是第一个字符,str[1] 是第二个... str[size-1] 是最后一个字符。 str 本身,没有指定任何字符,是一个指向您执行操作时创建的内存区域的指针

char str[size]

正如 Jerry 明确指出的那样,在 C 中,您不能以这种方式初始化数组。您需要从一个数组复制到另一个数组,因此您可以执行以下操作

strncpy(str, "\x80\xbb\x00\xcd", size); /* Copy up to size characters */
str[size-1]='\0'; /* Make sure that the string is null terminated for small values of size */

总结:区分指针、内存区域和数组非常重要。

祝你好运 - 我很确定您将在比您想象的更短的时间内掌握这些概念:)

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

char str[size]

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

strncpy(str, "\x80\xbb\x00\xcd", size); /* Copy up to size characters */
str[size-1]='\0'; /* Make sure that the string is null terminated for small values of size */

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 :)

凉城 2024-10-02 15:14:58

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.

心安伴我暖 2024-10-02 15:14:58

您不能使用=运算符分配数组内容。这只是 C 语言设计的一个事实。您可以在声明中初始化数组,但这

char str[size] = "\x80\xbb\x00\xcd";

与赋值是不同的操作。 请注意,在这种情况下,额外的“\0”将添加到字符串末尾。

“不兼容类型”警告来自于语言处理数组表达式的方式。首先,字符串文字存储为具有静态范围的 char 数组(意味着它们在程序的整个生命周期中都存在)。因此,字符串文字“\x80\xbb\x00\xcd”的类型是“4 5-element array of char”。然而,在大多数情况下,数组类型的表达式将隐式地从类型“T 的 N 元素数组”转换(“衰减”)为“指向 T 的指针”,并且表达式的值将是第一个的地址。数组中的元素。因此,当您编写该语句时,

str = "\x80\xbb\x00\xcd";

文字的类型已从“4 5-element array of char”隐式转换为“指向 char 的指针” >”,但赋值的目标是类型“100-element array of char”,并且这些类型不兼容(除此之外,数组表达式不能是 char 的目标代码>=运算符)。

要将一个数组的内容复制到另一个数组,您必须使用库函数,例如 memcpymemmovestrcpy 等。此外,对于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 as

char str[size] = "\x80\xbb\x00\xcd";

but 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 "4 5-element array of char". 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 statement

str = "\x80\xbb\x00\xcd";

the type of the literal was implicitly converted from "4 5-element array of char" to "pointer to char", but the target of the assignment is type "100-element array of char", 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, for strcpy 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.

错々过的事 2024-10-02 15:14:58

要将字符串文字分配给 str 数组,您可以使用字符串复制函数 strcpy

To assign a String Literal to the str Array you can use a the String copy function strcpy.

紫罗兰の梦幻 2024-10-02 15:14:58

char a[100] = "\x80\xbb\x00\xcd";char a[] = "\x80\xbb\x00\xcd";

char a[100] = "\x80\xbb\x00\xcd"; OR char a[] = "\x80\xbb\x00\xcd";

活泼老夫 2024-10-02 15:14:58

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文