简体   繁体   English

如何在C中打印字符串的二维数组(带空格)?

[英]How to print a two dimensional array of strings(with spaces) in C?

I am trying to print out a two dimensional array of strings using the array shown below and associate each phrase with a number from 0 to 3. When I try to print out each phrase the words get matched together and print out incorrectly. 我试图使用下面显示的数组打印出二维的字符串数组,并将每个短语与0到3之间的数字相关联。当我尝试打印每个短语时,单词匹配在一起并错误地打印出来。

char PhraseList[4][10]= {" Work Hard","Play Hard ","Enjoy","Live"};

How can I print out each phrase on a separate line so that " Work Hard" prints out on one line then "Play Hard " on another line and then "Enjoy" on another etc. Also how can I associate each phrase with a number? 如何在单独的行上打印每个短语,以便“努力工作”在一行上打印,然后在“演奏努力”在另一行上打印,然后在另一行“享受”,依此类推。另外,如何将每个短语与数字相关联? Any help\\suggestions will be greatly appreciated! 任何帮助\\建议将不胜感激!

Here is my code: 这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>



int main()
{
char PhraseList[4][10]= {" Work Hard","Play Hard ","Enjoy","Live"};

int i;

for(i=0; i<4; i++)
{
    printf("%s \n", PhraseList[i]);
}


printf("\n\n");
system("PAUSE");
return 0;
}

Output: 输出:

 Work HardPlay Hard Enjoy
Play Hard Enjoy
Enjoy
Live


Press any key to continue . . .

Your printf calls are fine. 您的printf电话很好。 The real problem is that you are overflowing the buffer. 真正的问题是缓冲区溢出。 Each string has been given 10 bytes maximum storage. 每个字符串已被赋予10个字节的最大存储量。 But in C strings are by definition NUL terminated. 但是在C字符串中,按定义NUL终止。 So you need an extra byte to store that NUL . 因此,您需要一个额外的字节来存储该NUL

Better not to specify a fixed size at all. 最好不要完全指定固定大小。 Can just do this: 可以这样做:

const char *PhraseList[]= {" Work Hard","Play Hard ","Enjoy","Live"};

As pointed out in the comment, the leading and trailing space in " Work Hard" and "Play Hard " respectively is the reason for issue. 正如评论中指出的那样,“努力工作”和“认真努力”中的前导和尾随空格是问题的原因。

The size of each of these is 11 characters (not 10). 这些字符的大小均为11个字符(而不是10个字符)。

" '[space]' 'W' 'o' 'r' 'k' '[space]' 'h' 'a' 'r' 'd' '\0'"

which results in 11 characters. 结果为11个字符。

Hence increase the size of PhraseList and declare it as 因此,增加PhraseList的大小并将其声明为

char PhraseList[4][11]= {" Work Hard","Play Hard ","Enjoy","Live"};

or 要么

const char *PhraseList[]= {" Work Hard","Play Hard ","Enjoy","Live"};

You allocate too little memory for your strings. 您为字符串分配的内存太少。

Lets look " Work Hard". 让我们来看看“努力工作”。 In memory this string is stored as ' ', 'W', 'o', 'r', 'k', ' ', 'H', 'a', 'r', 'd', '\\0', that is in it requires 11 bytes. 在内存中,此字符串存储为“”,“ W”,“ o”,“ r”,“ k”,“”,“ H”,“ a”,“ r”,“ d”,“ \\ 0”,它需要11个字节。

But you allocate 10 bytes only. 但是您只能分配10个字节。 Change type to PhraseList[4][11] . 将类型更改为PhraseList[4][11]

you could either get rid of your leading and trailing whitspaces or make you array bigger. 您可以摆脱领先和落后的惠特空间,也可以扩大规模。 If you want to output Number associated with the array just add the i variable to your output. 如果要输出与数组关联的Number,只需将i变量添加到输出中。

int main()
{
char PhraseList[4][10] = { "Work Hard","Play Hard","Enjoy","Live" };

int i;

for (i = 0; i<4; i++)
{
    printf("%d %s \n", i, PhraseList[i]);
}

printf("\n\n");
system("PAUSE");
return 0;
}
  • First Solution: 第一个解决方案:

  • Just remove space from your array's first and second string.Because it is exceded the araay string length. 只需从数组的第一和第二个字符串中删除空格即可,因为它超出了araay字符串的长度。

  • Second Solution: 第二种解决方案:

  • Just increase your array's string size, 只需增加数组的字符串大小,

  • Instead of this, 代替这个

     char PhraseList[4][10] = { " Work Hard","Play Hard ","Enjoy","Live" }; 
  • Declare it like this, 这样声明

     char PhraseList[4][15] = { " Work Hard","Play Hard ","Enjoy","Live" }; 

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

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