我正在开发一个简单的应用程序,用于根据文本生成铁栅栏密码。
应用程序正在运行,但是当它启动时,会出现一条消息,通知我应用程序已停止。
我可以检查问题出在哪里,但无法解决。
RailFence(char *originalText)
{
int key = 3;
char fenceText[3][200];
char encryptedText[200];
int i,j,x,y,z = 0;
int reverseIndex = 0;
int p = 0;
//Fill all de 2D array with 0
for (x = 0; x < key; x++)
{
for (z = 0; z < 200; z++)
{
fenceText[x][z] = '0';
}
}
/* Fill the 2D array like a fence
for exemple: the original text "I am very smart" with key "3" will be:
I e m
a v r s a t
m y r
*/
for (j = 0; j < strlen(originalText); j++) // <---- this block of is not working
{
if (reverseIndex == 0)
{
fenceText[i][j] = originalText[j];
i++;
}
else
{
i--;
fenceText[i][j] = originalText[j];
}
if (i == 0)
{
reverseIndex = 0;
i++;
}
else if (i == key)
{
reverseIndex = 1;
i--;
}
}
x = 0;
z = 0;
y = 0;
/* Here I fill the encryptedText reading each line from the 2d array "fenceText".
For example: The fence that I create above will be: "Iemavrsatmyr"
*/
for (x = 0; x < key; x++)
{
for (z = 0; z < 200; z++)
{
if(fenceText[x][z] != '0')
{
encryptedText[y] = fenceText[x][z];
y++;
}
}
}
//Just print the text already encrypted
for (p = 0; p < strlen(encryptedText); p++)
{
printf("%c", encryptedText[p]);
}
}
最佳答案
线路
int i,j,x,y,z = 0;
仅初始化 z
,因此使用 i
进行索引会导致未定义的行为。
关于c - C 中的二维数组不起作用 - 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18922083/