【问题标题】:C - Printing a 2D Char ArrayC - 打印二维字符数组
【发布时间】:2016-03-11 09:23:44
【问题描述】:

如何在 C 中打印二维字符数组的元素?

这是我当前的代码:

int main()
{
  unsigned int size;

  printf("Enter size:\n");
  scanf("%d",&size);

  char word[size][size];

  //Enter the matrix
  for(int k = 0; k < (size); ++k){
    for (int j = 0; j < (size); ++j){
      printf("Enter letter:");
      scanf("%c",&word[k][j]);
    }
  }

  //printf("\n");
  for (int k = 0; k < size; ++k){
    for(int j = 0; j < size; ++j){

      printf("%c",word[k][j]);
    }
    //printf("\n ");
  }
  printf("\n");
}

执行时,它会成对返回元素(使用 4x4 数组) 示例:

ab
cd
ef
gh
ij
kl
mn
op

而不是我想要的输出:

abcd
efgh
ijkl
mnop

这是为什么?

【问题讨论】:

  • 欢迎来到 Stack Overflow!听起来您可能需要学习如何使用调试器来逐步执行代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。进一步阅读:How to debug small programs.
  • 您直到最后才明确打印任何换行符,因此其余的可能来自您的输入。在按下回车键之前,您是否在一行中输入了所有值?如果不是,那么scanf 会将换行符存储在word 中。
  • 对 unsigned int 使用 %u 格式说明符

标签: c arrays char


【解决方案1】:

改变你的 scanf 可以解决所有问题

scanf(" %c",&word[k][j]);  // notice the space before '%c'

您还需要将打印循环更改为此

for (k = 0; k < size; ++k){
    for(j = 0; j < size; ++j){
        printf("%c",word[k][j]);
    }
    printf("\n");
}

【讨论】:

    【解决方案2】:

    注意:%c%1s 做不同的事情(除了为后者添加终止 null):

    • c 取每个字符包括空格、制表符、cr 和 lf
    • %1s 跳过所有空格(空格、制表符、cr、lf 等)

    所以在输入时,你应该使用:

    char c[2]; // provide room for a terminating null...
    ...
    for(int k = 0; k < (size); ++k){
        for (int j = 0; j < (size); ++j){
            printf("Enter letter:");
            scanf("%1s",c);
            word[k][j] = c[0];
        }
    }
    

    在打印时:

    for (int k = 0; k < size; ++k){
        for(int j = 0; j < size; ++j){
            printf("%c",word[k][j]);
        }
        printf("\n "); // new line after each line
    }
    

    【讨论】:

      【解决方案3】:

      我删除了读数,看起来打印还可以:

      int main()
      {
          const unsigned int size = 4;
          char word[size][size];
      
          //Enter the matrix
          for (int k = 0; k < (size); ++k) {
              for (int j = 0; j < (size); ++j) {
                  word[k][j] = 'a' + j + (k * size);
              }
          }
      
          for (int k = 0; k < size; ++k) {
              for (int j = 0; j < size; ++j) {
      
                  printf("%c", word[k][j]);
              }
              printf("\n");
          }
          printf("\n");
      
          getchar();
          return 0;
      }
      

      还有输出:

      abcd
      efgh
      ijkl
      mnop
      

      【讨论】:

        【解决方案4】:

        我发现您的来源有两个问题。

        一个是内存分配——其实不是ansi-c。

        如果您需要动态内存,则需要在运行时分配它。考虑改用 C++,因为有标准工具可以更安全地帮助实现这一点。

        第二个问题是缓冲区中有一个空白字符用作输入字符。我想你想澄清这一点。

        这是带有附加 cmets 的源代码:

        #include <stdio.h>
        #include <stdlib.h>
        
        void ansiC()
        {
            unsigned int size;
        
            printf("Enter size:\n");
            scanf("%d", &size);
        
            //char word[size][size]; <- this is not ansi-c because size is unknown at compile time
            char * word = (char*)malloc(sizeof(char)* size * size);
        
        
            //Enter the matrix
            for (int k = 0; k < (size); ++k)
            {
                for (int j = 0; j < (size); ++j)
                {
                    printf("Enter letter:");
                    scanf("%c ", &word[k * size + j]);
                    //since word is just a pointer i changed the way the position is calculated
                    //after the character the user presses the enter key
                    //this puts a whitespace character on the buffer. 
                    //by adding the space after %c you also clear that from the buffer
                }
            }
        
            //printf("\n");
            for (int k = 0; k < size; ++k)
            {
                for (int j = 0; j < size; ++j)
                {
        
                    printf("%c", word[k * size + j]);
                    //since word is just a pointer i changed the way the position is calculated
                }
                //printf("\n ");
            }
            printf("\n");
        
            free(word); //if you use malloc you need to remember to use free
        }
        
        int main()
        {
            ansiC();
            return 0;
        }
        

        【讨论】:

        • 我很欣赏这个涉及指针的响应。它实际上帮助我更好地理解他们!谢谢
        【解决方案5】:

        请检查一下。

        # include <iostream.h>
        # include <conio.h>
        
        
        void main()
        {
        clrscr();
        char arr[5][3]={"abc","aks","tny","dkn","kbf"};
            for(int a=0;a<5;a++)
            {
                for(int b=0;b<3;b++)
                {
                    cout<<" "<<arr[a][b];
                }
                cout<<endl;
            }
        
        
         getch();
        }
        

        【讨论】:

          猜你喜欢
          • 2017-12-29
          • 2014-12-21
          • 2015-12-13
          • 1970-01-01
          • 2018-09-29
          • 1970-01-01
          • 2018-12-02
          • 1970-01-01
          相关资源
          最近更新 更多