c - C 中的二维数组不起作用 - 运行时错误

标签 c arrays encryption multidimensional-array

我正在开发一个简单的应用程序,用于根据文本生成铁栅栏密码。

应用程序正在运行,但是当它启动时,会出现一条消息,通知我应用程序已停止。

我可以检查问题出在哪里,但无法解决。

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/

相关文章:

c - volatile const int 在 C 中的含义?

java - 快速排序算法,需要一些小的说明

arrays - 为什么禁止内置 "braced-init-list"中的 "operator[]"?

hadoop - 找不到带有 key dfs.encryption.key.provider.uri 的 uri 以在 CDH 5.4 的 HDFS 加密中创建 key 提供程序

c - 使用 cmake 编译时 Valgrind 不显示行号

c++ - ZigBee Arduino,解析数据不正确

c - 是否有类似开关的关键字可以接受字符串 (char *) 而不仅仅是 char?

arrays - Json 到数组与数组到字典

java - 如何解密 Jenkins 8mha 值

java - 使用方法加密java中的数字