将指向2d数组的指针转换回实际数组

时间:2014-06-29 06:13:10

标签: c++ arrays pointers

我已经在这方面工作了一段时间了。我通过程序的各个部分传递了一个2d数组,所以我需要使用指针,如下所示:

char **temparray(char realarray[ROWS][COLS], int ROWS, int COLS){
    char **temparr = new char*[ROWS];
    for (int i = 0; i < ROWS; i++){
        temparr[i] = new char[COLS];
        for (int j = 0; j < COLS; j++){
            int neighbors = matrixstuff(realarray,i,j);
            //do stuff that sets temparr[i][j] = something;
        }
    }
    return temparr;
}

这一部分有效。但我真正想要做的是创建一个循环,继续作用于数组。也就是说,我希望将temparr作为参数发送回temparray()。

问题是,当我检索更改的数组时,我只有指针,并且我无法将指针发送回此函数,因为我的matrixstuff()函数需要作用于2d数组。

所以我最终得到的错误是

"ISO C++ forbids comparison between pointer and integer [-fpermissive]"

基本上我想要一种从使用二维数组的函数返回一个实际的二维数组的方法(可疑,给我看过的其他帖子),或者一些关于如何创建一个函数的建议只需要一个指向2d数组的指针并遍历指针所指向的地址,以便重新创建原始数组的副本。我希望这很清楚。

谢谢!


编辑:以下是完整代码。这是一项任务。

#include <iostream>
#include <fstream>
#include <string>

const int ROWS = 40;
const int COLS = 80;

//incredibly annoying workaround for 2d array
//new to put array on heap so it doesn't get destroyed before returning

char **convert(std::string file){

char **arr = new char*[ROWS];

for(int i=0; i < ROWS; i++) {   //rows
    arr[i] = new char[COLS];
    for(int j=0; j < COLS; j++) {   //columns
        arr[i][j] = file.at(j); //puts value from file
    }
}
return arr;
}
//then need to free up previously allocated memory

void freearr(char **arr)
{
for (int i=0; i < ROWS; i++) {
    delete [] arr[i];}
delete [] arr;
}

/*
If an occupied cell has zero or one neighbors, it dies of loneliness.
If an occupied cell has more than three neighbors, it dies of overcrowding.
If an empty cell has exactly three occupied neighbor cells, a new cell is born.
*/

int countneighbors(char arr[ROWS][COLS],int i,int j){
int neighbors = 0;

if (arr[i - 1][j - 1] == true){
    neighbors++; }
if (arr[i - 1][j] == true){
    neighbors++; }
if (arr[i - 1][j + 1] == true){
    neighbors++; }
if (arr[i][j - 1] == true){
    neighbors++; }
if (arr[i][j + 1] == true){
    neighbors++; }
if (arr[i + 1][j - 1] == true){
    neighbors++; }
if (arr[i + 1][j] == true){
    neighbors++; }
if (arr[i + 1][j + 1] == true){
    neighbors++; }
return neighbors;
}

char **temparray(char realarray[ROWS][COLS], int ROWS, int COLS){
char **temparr = new char*[ROWS];
for (int i = 0; i < ROWS; i++){
    temparr[i] = new char[COLS];
    for (int j = 0; j < COLS; j++){
        int neighbors = countneighbors(realarray,i,j);
        if ((neighbors < 2) || (neighbors > 3)){
            temparr[i][j] = 32;};
        if ((realarray[i][j] == 32) && (neighbors == 3)){
            temparr[i][j] = 42;};
        if ((realarray[i][j] == 32) && (neighbors == 2)){
            temparr[i][j] = 32;};
        if ((realarray[i][j] == 42) && (neighbors == (2 || 3))){
            temparr[i][j] = 42;};
    }
}
return temparr;
}

void freetemparray(char **temparr)
{
for (int i=0; i < ROWS; i++){
    delete [] temparr[i];}
delete [] temparr;
}

void display(char **workingarray){
char **lastarr = new char*[ROWS];
for (int i = 0; i < ROWS; i++){
    lastarr[i] = new char[COLS];
    for (int j = 0; j < COLS; j++){
        std::cout << lastarr[i][j];
    std::cout << std::endl;
    }
}
for (int i=0; i < ROWS; i++) {
    delete [] lastarr[i];}
delete [] lastarr;
}

int main () {

std::string file;
std::cout << "Please enter file name: " << std::endl;
getline(std::cin, file);
int rounds;
std::cout << "Please enter iterations: " << std::endl;
std::cin >> rounds;

char** workingarray = convert(file);
//freearr(arr); //not quite sure why this doesn't work

for (int i = 0; i < rounds; i++){
    char temporary = temparray(workingarray, ROWS, COLS); //problems with conversion
    char workingarray = temporary;
    //freetemparray(**temparr);
}

display(workingarray);

return 0;
}

完整错误如下:

-------------- Build: Debug (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions  -g  -std=c++11    -c /main.cpp -o obj/Debug/main.o
/main.cpp: In function ‘int main()’:
/main.cpp:116:60: error: cannot convert ‘char**’ to ‘char (*)[80]’ for argument ‘1’ to ‘char** temparray(char (*)[80], int, int)’
         char temporary = temparray(workingarray, ROWS, COLS); //problems with conversion
                                                            ^
/main.cpp:117:14: warning: unused variable ‘workingarray’ [-Wunused-variable]
         char workingarray = temporary;
              ^
Process terminated with status 1 (0 minutes, 1 seconds)
1 errors, 1 warnings (0 minutes, 1 seconds)

0 个答案:

没有答案