简体   繁体   中英

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


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.

Then the string literal is actually an array of four characters (the characters 'h' , 'a' , 'i' and the string terminator '\\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.

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

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

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.

The type of "hai" is a const char[4] . Although in certain instances it does decay to a const char* .

You are attempting to assign a const char[4] type to a char type. That's a type mismatch and the compiler will disallow that.

You could, however, write strncpy(a[0], "hai", 5); where 5 is the upper limit of the number of characters that can be copied from the string literal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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