将值分配给2D数组,而无需在C 中使用索引

Assigning values to 2d arrays without using index in c++

本文关键字:索引 分配 2D 数组      更新时间:2023-10-16

作为挑战,我不使用数组索引分配或搜索。我想做的是从文本文件中读取字符,然后将它们放入15x15的数组中。我使用索引创建了一个2D数组,但是没有它们就无法进行分配。我尝试了两件事,但我无法提出一个工作的想法。我的代码基本上是这样:

        char **puzzleArray;
        puzzleArray = new char*[15];
        for (int i = 0; i < 15; i++)
            puzzleArray[i] = new char[15];

        char *c;
        FILE* puzzle;
        puzzle = fopen("puzzle.txt", "r");

        for (int i=0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                fread(&c, sizeof(char), 1, puzzle);
                if (*c != ' ' && *c != 'n')
                    strcpy(*(puzzleArray + (i * 15)) + j , c);
                else
                    j--;
            }

我也尝试过这样的分配:

        char **puzzleArray;
        puzzleArray = new char*[15];
        for (int i = 0; i < 15; i++)
            puzzleArray[i] = new char[15];

        char c;
        FILE* puzzle;
        puzzle = fopen("puzzle.txt", "r");

        for (int i=0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                fread(&c, sizeof(char), 1, puzzle);
                if (c != ' ' && c != 'n')
                    *(puzzleArray + (i * 15)) + j = c;
                else
                    j--;
            }

但是它们都没有起作用,当我做第二个编译器时,我会给我错误的错误"作为分配的左操作数所需的lvalue"。

编辑:当我更改第二个代码为:`

*(*(puzzleArray + (i * 15)) + j)

它编译了,但是当我尝试使用以下方式打印数组中的字符时:

for (int i = 0; i < 15; i++) {
    cout << endl;
    for (int j = 0; j < 15; j++)
        cout << *(*(puzzleArray + (i * 15)) + j);
}
//or using this
for (int i = 0; i < 15; i++) {
    cout << endl;
    for (int j = 0; j < 15; j++)
        cout << puzzleArray[i][j];
}

它不会打印字符和程序崩溃。在Visual Studio中,它说一个例外发生在分配部分。

您不需要嵌套分配。更重要的是,您的代码不起作用,因为您假设嵌套分配将导致连续存储。

const int size = 15;
char* puzzleArray = new char[size*size];
// i'm not going to change file reading logic, 
// but it should be done better
// ideally by one read to a buffer
// Also the style seems like a bad mix of C and C++,
// but I can't do much about it with only this little fragment of code.
FILE* puzzle = fopen("puzzle.txt", "r");

for (int i=0; i < size; i++) {
    for (int j = 0; j < size; j++) {
        char c;
        fread(&c, 1, 1, puzzle);
        if (c != ' ' && c != 'n')
            puzzleArray[i*size + j] = c;
        /* 
        // this does not seem right, i don't know what you're trying to do here
        // i'm gonna assume that you want to move to the next line
        else
            j--;
        */
        else
            break;
    }
}

第二次尝试有两个问题。以及记忆是否连续不相关。您仍然可以使用2D数组并使用嵌套分配。(请注意,尽管有人向我的答案投下了投票,但我认为他/她做错了)。

这是使用2D数组和嵌套内存分配的全面测试的代码。

    #include <iostream>
    #include <cstdlib>
    int main() {
        using namespace std;
        char **puzzleArray;
        puzzleArray = new char*[15];
        for (int i = 0; i < 15; i++)
            puzzleArray[i] = new char[15];
        char c;
        FILE* puzzle;
        puzzle = fopen("puzzle.txt", "r");

        for (int i=0; i < 15; i++) {
            for (int j = 0; j < 15; ) {
                fread(&c, sizeof(char), 1, puzzle);
                if (c != ' ' && c != 'n') {
                    *(*(puzzleArray + i) + j) = c; // you can see the difference if you compare with your original code.
                    ++j;
                }
            }
        }

        for (int i = 0; i < 15; ++i) {
            for (int j = 0; j < 15; ++j) {
                cout << puzzleArray[i][j]; // you could use pointer arithmatic here too. using index is just convenient to show the result.
            }
            cout << std::endl;
        }
    }