C - 打印2D字符阵列

时间:2021-04-15 21:34:37

How do I print the elements of a 2D Char Array in C?

如何在C中打印2D字符数组的元素?

Here is my current code:

这是我目前的代码:

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

When executed it returns the element in pairs (using a 4x4 array) Example:

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

ab
cd
ef
gh
ij
kl
mn
op

Rather than my desired output:

而不是我想要的输出:

abcd
efgh
ijkl
mnop

Why is this?

为什么是这样?

5 个解决方案

#1


3  

changing your scanf solves all the problems

改变你的scanf解决了所有问题

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

And also you need to change your printing loop to this

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

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

#2


1  

I removed the reading and it seems like printing is ok:

我删除了阅读,似乎打印是好的:

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

And the output:

并输出:

abcd
efgh
ijkl
mnop

#3


1  

I found two issues with your source.

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

One is the memory allocation - that is actually not ansi-c.

一个是内存分配 - 实际上并非ansi-c。

If you need dynamic memory you need to allocate it at runtime. Consider switching to c++ since there are standard facilities that help with that in a safer way.

如果需要动态内存,则需要在运行时分配它。考虑切换到c ++,因为有标准的设施以更安全的方式帮助它。

The second issue was that there is a whitespace character in the buffer that is used as an input character. I think you want to clear that.

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

Here is the source with additional comments:

以下是包含其他注释的来源:

#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;
}

#4


0  

Beware: %c and %1s do different things (apart from adding a terminating null for the latter):

注意:%c和%1s执行不同的操作(除了为后者添加终止空值):

  • c take every character including space, tab, cr and lf
  • c取每个角色,包括空格,制表符,cr和lf

  • %1s skip over all blanks (space, tab, cr, lf, etc.)
  • %1s跳过所有空格(空格,制表符,cr,lf等)

So at input time, you should use:

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

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];
    }
}

And at print time:

在打印时:

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
}

#5


0  

Please check this .

请检查一下。

# 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();
}

#1


3  

changing your scanf solves all the problems

改变你的scanf解决了所有问题

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

And also you need to change your printing loop to this

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

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

#2


1  

I removed the reading and it seems like printing is ok:

我删除了阅读,似乎打印是好的:

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

And the output:

并输出:

abcd
efgh
ijkl
mnop

#3


1  

I found two issues with your source.

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

One is the memory allocation - that is actually not ansi-c.

一个是内存分配 - 实际上并非ansi-c。

If you need dynamic memory you need to allocate it at runtime. Consider switching to c++ since there are standard facilities that help with that in a safer way.

如果需要动态内存,则需要在运行时分配它。考虑切换到c ++,因为有标准的设施以更安全的方式帮助它。

The second issue was that there is a whitespace character in the buffer that is used as an input character. I think you want to clear that.

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

Here is the source with additional comments:

以下是包含其他注释的来源:

#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;
}

#4


0  

Beware: %c and %1s do different things (apart from adding a terminating null for the latter):

注意:%c和%1s执行不同的操作(除了为后者添加终止空值):

  • c take every character including space, tab, cr and lf
  • c取每个角色,包括空格,制表符,cr和lf

  • %1s skip over all blanks (space, tab, cr, lf, etc.)
  • %1s跳过所有空格(空格,制表符,cr,lf等)

So at input time, you should use:

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

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];
    }
}

And at print time:

在打印时:

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
}

#5


0  

Please check this .

请检查一下。

# 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();
}