如何在C中向二维数组添加字符串

我开始学习C并且我遇到了将字符串输入添加到2D数组的问题,我能够正确地获取字符串输入但是当我尝试将字符串元素添加到数组时它不能按预期工作当打印数组时(这是我测试程序的方式),它会为数组中的每个索引分配一个字符而不是整个字符串。

这是我的观看代码,非常感谢您提前感谢任何发布的帮助。

#include  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]=! 

当你声明: char c_array [3] [3];

这意味着2D数组的每个元素都是“char”(单个字符); 不是“一串字符”。

要使每个元素都是一个字符串,您需要按以下方式声明您的数组:char string_array [3] [3] [256];

我不确定这是你想要做的。 Mw的感觉是你需要一个包含3个元素的数组,其中每个元素都是一个字符串。 在这种情况下,[3]是不够的,你的字符串必须少于两个字符(第三个字符保留给终止零)。

字符串不是一种类型。 它们是一个值模式,就像你说int存储十的倍数,然后它以0结尾…如果你说一个char数组存储一个字符串,那么它在第一个'\0' ,见? 我们可以在不同类型的整数变量中存储十的倍数,对于字符串也是如此。

字符串是值的模式,而不是类型。 在选择我们想要使用的整数类型时,我们考虑整数的范围。 我们为小整数值(小于32768)选择int ,对于较大的值(小于2147483648)选择int ,或者对于大于该值的值选择long long 。 因此,我们根据预期的十倍数选择正确的整数类型。 同样对于字符串,我们需要确保我们有足够的内存用于字符串中的字符,最后是'\0'

字符&c_array[x][y]只有足够的内存用于0个字符,后跟一个'\0' ; 它只对存储空字符串有用。 也许你打算像这样声明c_array

 char c_array[3][3][256]; 

在这种情况下, scanf期望%s对应于char *类型的参数…但是由于编译器可能会警告您, &c_array[x][y]将具有char (*)[256] 。 如果要修复该警告,则需要从scanf代码中删除&

 if (scanf("%s", c_array[x][y]) != 1) { /* XXX: Handle an error here, * * eg by exiting or returning or something */ } 

当我们谈论这个话题时,你会注意到我删除了多余的空间; %s已经执行了该function format指令会执行。 我还编写了代码来检查scanf的返回值,你应该……


还记得我们如何谈论选择用于存储数据的整数类型吗? 另一个考虑因素是我们打算存储的数据是否可以是负数 。 考虑一下:在你的代码中, xy应该能够存储负值吗? 负数组索引有什么意义? 建议将所有数组索引声明为size_t 。 当您使用printf打印size_t值时,您将需要使用%zu格式说明符而不是%d


 char c_name[256]; /* ... */ printf("The string is %s\n",c_name); 

现在,如果字符串存储在c_array[x][y] ,那么c_name的意义是c_name ? 这是一个不必要的变量。 请改用c_array[x][y]


 printf("\nPlease enter a name to the system:"); 

常见的实现通常会在写入'\n'之前避免打印字符; 该行一次打印而不是逐字符打印。 因此,您可能会看到一些奇怪的行为,例如此行未在正确的时间出现。 通过将'\n'放在行的末尾而不是开头来解决此问题:

 printf("Please enter a name to the system:\n"); 

…或者使用puts ,它会自动为您添加’\ n’:

 puts("Please enter a name to the system:"); 

…或使用fflush ,即使没有'\n' ,也会写入所有未决的未写入数据:

 fflush(stdout); 

如果要为数组中的每个索引分配一个字符串,请按如下方式创建数组:

 #include int main(void) { char a[2][10]; int i=0; char temp[20]; for(i=0;i<2;i++) { scanf("%s",temp); strcpy(a[i],temp); } printf("%s",a[0]); return 0; } 

您输入的每个字符串都将存储在数组的每个索引中。

在你的代码中

 scanf(" %s",&c_array[x][y]); printf("The string is %s\n",c_name); 

这两个陈述都是错误的。

  1. %s需要一个指向数组的指针,而不是一个char 。 要扫描单个char ,您需要%c格式说明符。
  2. printf()使用的c_name未初始化。 使用未初始化的值会调用未定义的行为 。

解决方案 :要逐个元素地输入,你可以做类似的事情

 for(x=0; x<=2 ;x++) { printf("Start entering the name, one character at a time\n"); for(y=0; y< 2; y++) //note only '<' here { scanf(" %c", &c_array[x][y]); // note the leading space } c_array[x][y] = 0; //null termination of array to be used as string } 
 #include  #include  int main(void){ char c_array[3][3]; int x; int y; //int z=10;//unused int i_user_input; char c_name[256]; printf("\nPlease enter a name to the system:"); scanf("%255s",c_name); 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) ; else if(i_user_input==2){ memcpy(c_array, c_name, sizeof(c_array)); for(x=0;x<3;x++){ for(y=0;y<3;y++){ printf("c_array[%d][%d]=%c\n", x, y, c_array[x][y]); } } } } 

 #include  #include  #include  #define SIZE 3 int main(void){ char *c_array[SIZE][SIZE] = { NULL }; int x; int y; //int z=10;//unused int i_user_input; char c_name[256]; for(x=0;x 

为什么不在数组/矩阵中使用指针?

 #include  #include  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_name); new_string = malloc(strlen(c_name)+1) if (new_string == NULL) { print("error allocating string\n") exit(-1); } strcpy(new_string, c_name); c_array[x][y] = new_string 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]); } } } } for(x=0;x<=2;x++) { for(y=0;y<=2;y++) { free(c_array[x][y]) } } } 

如果这是您的目标,上面的代码将输入字符串存储在数组的每个单元格中。

注意:我没有尝试过,所以可能会有细微的错误。 我的观点是向您展示如何使用指针及其与数组的等价性。 使用没有指针的C是没有意义的。 🙂