【发布时间】:2015-06-11 07:50:08
【问题描述】:
我开始学习 C 并且遇到了将字符串输入添加到 2D 数组的问题,我能够正确获取字符串输入,但是当我尝试将字符串元素添加到数组时它不是按预期工作。打印数组时(这是我测试程序的方式),它将为数组中的每个索引分配一个字符而不是整个字符串。
这是我的查看代码,非常感谢您提前发布任何帮助。
#include <stdio.h>
main()
{
char c_array[3][3];
int x;
int y;
int z=10;
int i_user_input;
char c_name[256];
for(x=0;x<=+2;x++)
{
for(y=0;y<=2;y++)
{
printf("\nPlease enter a name to the system:");
scanf(" %s",&c_array[x][y]);
printf("The string is %s\n",c_name);
printf("Please press '1' if you would like to keep entering names\n");
printf("Please press '2' if you would like to print the list of names:");
scanf("%d",&i_user_input);
if(i_user_input==1)
continue;
if(i_user_input==2)
for(x=0;x<=2;x++)
{
for(y=0;y<=2;y++)
{
printf("c_array[%d][%d]=%c\n",x,y,c_array[x][y]);
}
}
}
}
}
示例输入“Penelope!”的输出如下所示
c_array[0][0]=P
c_array[0][1]=e
c_array[0][2]=n
c_array[1][0]=e
c_array[1][1]=l
c_array[1][2]=o
c_array[2][0]=p
c_array[2][1]=e
c_array[2][2]=!
【问题讨论】: