为什么不能将字符串值分配给2d char数组? [英] why we can't assign a string value to 2d char array?

查看:43
本文介绍了为什么不能将字符串值分配给2d char数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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?

推荐答案

用于只读访问的另一个选项(如果您不修改字符串文字)是指针数组:

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";

这篇关于为什么不能将字符串值分配给2d char数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆