2D char数组在构建时看起来很好但后来成为一个大的连续混乱

时间:2014-04-07 09:34:16

标签: unix gcc

在分配期间一切看起来都很好但是当我在外面访问结果时,我得到了所有数组元素的串联..

#include<string.h>
#include<fcntl.h>
#include<stdio.h>
int sqr(int r)
{
        int i,temp=1;
        for(i=0;i<r;i++)
        {
                temp*=2;
        }
        return temp;
}
char arrbin[20][8];
void  toBinary(int ascii)
{
        int i,j,len=0,tascii=ascii;
        char binary[8]={""};
        strcpy(binary,"");
        while(1)
        {
                int bit=ascii%2;
                char bit1[2];
                snprintf(bit1,sizeof(bit1),"%d",bit);
                strcat(binary,bit1);
                if(ascii!=1)
                        ascii=ascii/2;
                if(ascii==1)
                        break;
        }
        if(tascii!=1)
                strcat(binary,"1");
        len=strlen(binary);
        for(i=0;i<(8-len);i++)
        {
                strcat(binary,"0");
        }
        printf("\n%s",binary);
        strcpy(arrbin[tascii-1],binary);
        strcat(arrbin[tascii-1],"\0");

}
}
void toBin(int bits)
{
        int i;
        for(i=0;i<bits;i++)
        {
                toBinary(i+1);
        }
}
void disp(int bits)
{
        int i;
        printf("\n%d:",bits);
        for(i=0;i<bits;i++)
                printf("\n\n%s",arrbin[i]);
                //hear print all element concat and return output
}
int main()
{
        char bin[100]={""},arr[100];
        int r=0;
        int val[10];
        int m;//length
        int temp=0;//for the assign value to  position
        int i,j,bits;//how many box are there
        system("clear");
        printf("Enter Binary:");
        scanf("%s",bin);
        m=strlen(bin);
        while((m+r+1)>sqr(r))
        {
                r++;
        }
        bits=m+r;
        printf("m:%d\nr:%d \n-----\nbits=%d\n",m,r,bits);
        for(i=0;i<r;i++)
                val[i]=sqr(i);
        for(i=0;i<bits;i++)
        {
                for(j=0;j<r;j++)
                {
                        if((i+1)==val[j])
                        {
                                arr[i]='r';
                  break;
                        }
                }
                if(arr[i]!='r')
                        arr[i]=bin[temp++];
        }
        toBin(bits);
        disp(bits);

        for(i=0;i<bits;i++)
                printf("%c ",arr[i]);
        getchar();
        return 0;
}   

当我在toBinary()中构建数组时,一切看起来都不错。但后来在disp()所有二维数组打印输出如

输出:

Enter Binary:1001001001001
m:13
r:5
-----
bits=18

100000000
010000000
110000000
001000000
101000000
011000000
111000000
000100000
100100000
010100000
110100000
001100000
101100000
011100000
111100000
000010000
100010000
010010000
18
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
01000000110000000010000010100000011000001110000000010000100100000101000011010000001100001011000001110000111100000000100010001000010010000
r r 1 r 0 0 1 r 0 0 1 0 0 1 0 r 0 1 

可能有什么不对?

1 个答案:

答案 0 :(得分:1)

您忘记使用\0字节终止单个条目。

  

字符串是由第一个零代码终止的连续代码单元序列(写入&#39; \ 0&#39;并且对应于ASCII空字符)。

source: Wikipedia

在构建数组时,这并不重要,因为您始终只打印最后一个元素。

另一个错误:arrbin[1]中的disp()应该是arrbin[i]

提示:在寻找错误时,请先尝试减少问题。

您可能还想阅读:http://en.wikipedia.org/wiki/Off-by-one_error