简体   繁体   English

为什么我们不能将字符串值赋给2d char数组?

[英]why we can't assign a string value to 2d char array?

#include<stdio.h>
int main()
{
char a[3][5];
int i;

a[0][5]="hai";
a[1][5]="cool";
a[2][5]="many";

for(i=0;i<3;i++)
printf("%s \n",a[i]);
return 0;
}

Why we cant assign string value like this but it can be assigned using string function? 为什么我们不能像这样分配字符串值,但它可以使用字符串函数分配?

You have three problems. 你有三个问题。 The first is that you attempt to assign a pointer to a single char . 第一个是您尝试将指针指定给单个char The second problem is that the single character you try to assign to is out of bounds. 第二个问题是您尝试分配的单个字符超出范围。

The third problem is that you can not assign to an array, only copy to it. 第三个问题是你不能分配给一个数组,只能复制到它。

You can solve all three problems by copying the string into the array, using the strcpy function: 您可以使用strcpy函数将字符串复制到数组中来解决所有这三个问题:

strcpy(a[0], "hai");

Be careful not to copy a string that is to long to fit in the destination array though, as that will then write out of bounds and lead to undefined behavior. 注意不要复制长到适合目标数组的字符串,因为这样会写出超出界限并导致未定义的行为。 The source can not be longer than four characters (five with the terminator). 源不能超过四个字符(五个带终结符)。 The can be solved by using strncpy instead, but that function have another problem that can leave the destination string unterminated instead. 可以通过使用strncpy来解决,但该函数有另一个问题,可能会使目标字符串无法终止。


To clarify: In the assignment a[0][5] = "hai" the expression a[0][5] is single character, but it is also undefined behavior since index 5 is out of bounds. 澄清:在赋值a[0][5] = "hai" ,表达式a[0][5]是单个字符,但它也是未定义的行为,因为索引5超出界限。

Then the string literal is actually an array of four characters (the characters 'h' , 'a' , 'i' and the string terminator '\\0' ). 然后字符串文字实际上是一个包含四个字符的数组(字符'h''a''i'和字符串终止符'\\0' )。 When using the string literal in an expression like your assignment, it decays to a pointer to its first element. 在类似于赋值的表达式中使用字符串文字时,它会衰减到指向其第一个元素的指针。 Therefore "hai" could be seen as a pointer. 因此, "hai"可以被视为指针。

So in a[0][5] = "hai" you assign a pointer to the letter 'h' in the string, to the single and out of bounds character a[0][5] . 因此, a[0][5] = "hai"您将指向字符串中字母'h'指针指向单个和越界字符a[0][5]

Another option for read only access (if you don't modify the string literal) is an array of pointers: 只读访问的另一个选项(如果你不修改字符串文字)是一个指针数组:

char *a[3]; /* Or better yet: const char *a[3]; */

a[0]="hai";
a[1]="cool";
a[2]="many";

First and foremost, you should check the datatypes for the operands of the assignment operators. 首先,您应该检查赋值运算符的操作数的数据类型。

On the LHS, you have a char , and you're trying to assign a char * (RHS) to it. 在LHS上,你有一个char ,你正在尝试为它分配一个char * (RHS)。 This is type mismatch, hence not possible. 这是类型不匹配,因此不可能。

That said, a being a two-dimensional array, you cannot even assign to a[0] either, as arrays ( not array elements ) are not assignable. 这就是说, a是一个二维阵列,甚至不能分配a[0]或者,作为阵列( 未数组元素 )是不可转让。

You have to copy the string literal into the memory, however, you have to do something like 你必须将字符串文字复制内存中,但是,你必须做类似的事情

 strcpy(a[0], "hai");

So, to answer, 所以,回答,

....but it can be assigned using string function? ....但它可以使用字符串函数分配?

Well, there's a difference between assignment using assignment operator and copying using the string-family library function. 嗯,使用赋值运算符赋值和使用字符串族库函数进行复制之间存在差异。 They don't behave the same way. 它们的行为方式不同。 Assignment using assignment operator to an array is not possible, as it needs a modifiable lvalue as the left operand, and an array (name) is not a modifiable lvalue. 使用赋值运算符赋值给数组是不可能的,因为它需要一个可修改的左值作为左操作数,而数组(name)不是可修改的左值。

The type of "hai" is a const char[4] . "hai"类型const char[4] Although in certain instances it does decay to a const char* . 虽然在某些情况下它会衰减const char*

You are attempting to assign a const char[4] type to a char type. 您正在尝试将const char[4]类型分配给char类型。 That's a type mismatch and the compiler will disallow that. 这是一种类型不匹配 ,编译器将不允许这样做。

You could, however, write strncpy(a[0], "hai", 5); 但是,你可以写strncpy(a[0], "hai", 5); where 5 is the upper limit of the number of characters that can be copied from the string literal. 其中5是可以从字符串文字中复制的字符数的上限。

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

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