2-d字符数组在C的意外输出。

时间:2021-02-16 19:59:20

I have this Piece of COde that store a string in a 2-d char array.IN my code i am using a 2x6 2-D char array.On providing a 12digit string LIKE > "COMEHOMEARUN". It should store it as
C O M E H O
M E A R U N
but I am getting the output as
C O M E H M
M E A R U N ...i.e the value at [0]6] automatically gets the value of [1][0].

我有这段代码,它将一个字符串存储在一个2-d字符数组中。在我的代码中,我使用的是一个2x6 2d字符数组。提供一个12位数的字符串,如>“COMEHOMEARUN”。它应该把它存储为C O M E H O M E A R U N但我得到的输出是C O M E H M E A R U N, I。e的值[0]6]自动得到[1][0]的值。

here's the code

这是代码

#include<stdio.h>
#include<conio.h>
void main()
{
    char string[20];
    char aray[1][5];
    int i,j,k=0;
    gets(string);
    //storing the individual characters in the string in the form of 2x6 char array
    for(i=0;i<=1;i++)
    {
        for(j=0;j<=5;j++)
        {
            aray[i][j]=string[k];
            k++;
        }
    }
    //displaying the array Generated
    for(i=0;i<=1;i++)
    {
        for(j=0;j<=5;j++)
        {
            printf("%c ", aray[i][j]);
        }
        printf("\n");
    }
    getch();
}

Does anybody know where I am going wrong?

有人知道我哪里出错了吗?

3 个解决方案

#1


4  

In a C array declaration like char array[N][M], the N and M values are not "the highest valid index"; they mean "the number of values".

在像char数组[N][M]这样的C数组声明中,N和M值不是“最高有效索引”;它们的意思是“价值的数量”。

So your declaration

所以你的宣言

char aray[1][5];

defines an array sized 1x5, not 2x6 as you intend.

定义一个数组大小为1x5,而不是您想要的2x6。

You need:

你需要:

char aray[2][6];

But of course, the actual indexing is 0-based so for char aray[2][6], the "last" element is at aray[1][5].

当然,实际的索引是基于0的,因此对于char aray[2][6],“最后”元素在aray[1][5]。

#2


0  

You can try by changing char aray[1][5] to char aray[2][6]

您可以尝试将char aray[1][5]改为char [2][6]

#3


0  

Your indexing is not correct.

你的索引不正确。

When you declare any char array you have to give sufficient length. In your code you give the dimension 1 but it required 2.

当你声明一个字符数组时,你必须给出足够的长度。在你的代码中,你给出了维度1,但它需要2。

Declare the array as:

声明数组:

   char array[2][6];

That will work.

这将工作。

#1


4  

In a C array declaration like char array[N][M], the N and M values are not "the highest valid index"; they mean "the number of values".

在像char数组[N][M]这样的C数组声明中,N和M值不是“最高有效索引”;它们的意思是“价值的数量”。

So your declaration

所以你的宣言

char aray[1][5];

defines an array sized 1x5, not 2x6 as you intend.

定义一个数组大小为1x5,而不是您想要的2x6。

You need:

你需要:

char aray[2][6];

But of course, the actual indexing is 0-based so for char aray[2][6], the "last" element is at aray[1][5].

当然,实际的索引是基于0的,因此对于char aray[2][6],“最后”元素在aray[1][5]。

#2


0  

You can try by changing char aray[1][5] to char aray[2][6]

您可以尝试将char aray[1][5]改为char [2][6]

#3


0  

Your indexing is not correct.

你的索引不正确。

When you declare any char array you have to give sufficient length. In your code you give the dimension 1 but it required 2.

当你声明一个字符数组时,你必须给出足够的长度。在你的代码中,你给出了维度1,但它需要2。

Declare the array as:

声明数组:

   char array[2][6];

That will work.

这将工作。