Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 无法正确生成表格_C - Fatal编程技术网

C 无法正确生成表格

C 无法正确生成表格,c,C,我正在为vigenere密码构建一个tabla recta表,出于某种原因,它在第一个循环后打印'@',而不是'a'。这是我的密码: #include <stdio.h> int main (void) { char alph[26] = "abcdefghijklmnopqrstuvwxyz"; char tRecta[26][26] = {0}; //Tabular Recta int i,k,j = 0; for(i=0;i<26;

我正在为vigenere密码构建一个tabla recta表,出于某种原因,它在第一个循环后打印
'@'
,而不是
'a'
。这是我的密码:

#include <stdio.h>

int
main (void)
{
    char alph[26] = "abcdefghijklmnopqrstuvwxyz";
    char tRecta[26][26] = {0};  //Tabular Recta

    int i,k,j = 0;
    for(i=0;i<26; i++) { //Build tabular recta
        for(k=0; k<26; k++) {
            if((j+k) > 26) {
                tRecta[i][k] = alph[(j+k)-26];
            } else {
                tRecta[i][k] = alph[(j+k)];
            }
        }
        j++;
    }

    for(i=0;i<26;i++) {
        for(k=0;k<26;k++) {
            printf("%c",tRecta[i][k]);
        }
        printf("\n");
    }
    return 0;
}

我以前很少使用C,is
printf(“%C”,tRecta[I][k])没有完全执行我认为它正在执行的操作?

您无法访问阵列。将
if((j+k)>26)
更改为
if((j+k)>=26)
,因为数组
alph
的最后一个有效索引是
25
,而不是
26
。另一种解决方案是使用
%
操作:

#define ARR_SIZE 26

int i,k,j = 0;
for( i=0; i<ARR_SIZE; i++ ) { //Build tabular recta
    for( k=0; k<ARR_SIZE; k++ ) {
        tRecta[i][k] = alph[ (j+k) % ARR_SIZE ];
    }
    j++;
}
#定义ARR#U尺寸26
int i,k,j=0;

对于(i=0;我不使用幻数!使用数组的大小作为限制有什么错?我建议如果((j+k)>25{而不是if((j+k)>26){,这应该可以解决问题。我建议将变量声明更改为:char alph[27]=“abcdefghijklmnopqrstuvxyz”;谢谢,为什么它在我的原始代码中打印@?如果它访问alph@user3831011
alph[26]
是数组
alpha
之后内存中的第一个字节。在您的情况下,它是随机的
@
,但这是未定义的行为。
#define ARR_SIZE 26

int i,k,j = 0;
for( i=0; i<ARR_SIZE; i++ ) { //Build tabular recta
    for( k=0; k<ARR_SIZE; k++ ) {
        tRecta[i][k] = alph[ (j+k) % ARR_SIZE ];
    }
    j++;
}