如何将二维数组定义为指针?C++记忆游戏动态内存分配报错求助
问题分析与解决方案
你的核心问题是变量重定义+指针类型混乱:全局区已经定义了char* ptr1,后续又定义了char (*ptr1)[SIZE][SIZE] = &chart,不仅重定义了变量,两种指针类型完全不兼容,直接触发编译错误。另外原代码中的固定大小数组chart[SIZE][SIZE]没必要保留,直接用动态分配的指针替代即可。
下面是修改后的完整代码,将所有固定数组替换为动态分配指针,同时修正指针操作逻辑:
#include <iostream> #include <string> #include <stdlib.h> #include <windows.h> #include <ctime> using namespace std; // 保留必要全局变量 int num, number = 1, a, b, x, y, startValue = 0, points = 0, chartSize, openCards = 0; // 动态二维指针,替代原固定数组 char** chart = nullptr; char** letters = nullptr; int** assigned = nullptr; void dynamicChart() { srand(time(NULL)); cout << "Please enter the size of the chart(2,4,6,etc.):" << endl; cin >> chartSize; while (chartSize % 2 != 0 || chartSize <= 0) { cout << "Invalid chart size.Please enter another value.\n"; cin >> chartSize; } // 动态分配二维数组:先分配行指针 chart = new char*[chartSize]; letters = new char*[chartSize]; assigned = new int*[chartSize]; // 分配每行元素并初始化 for (int i = 0; i < chartSize; ++i) { chart[i] = new char[chartSize]; letters[i] = new char[chartSize]; assigned[i] = new int[chartSize](); // 初始化所有元素为0 } int spaces = chartSize * chartSize; while (spaces != 0) { char rl = 'A' + rand() % 26; int count = 0; while (count < 2) { int r1 = rand() % chartSize; int r2 = rand() % chartSize; if (!assigned[r1][r2]) { letters[r1][r2] = rl; assigned[r1][r2] = 1; count++; } } spaces -= 2; } // 初始化游戏面板为* for (int i = 0; i < chartSize; ++i) { for (int j = 0; j < chartSize; ++j) { chart[i][j] = '*'; } } } void drawChart() { cout << "\n\n\n\n"; cout << " "; for (int i = 1; i < chartSize + 1; ++i) { cout << i << "\t "; } cout << "\n"; for (int i = 0; i < chartSize; ++i) { cout << number++; for (int j = 0; j < chartSize; ++j) { cout << "| " << chart[i][j] << "\t"; } cout << endl; } number = 1; } void mainMenu() { cout << "Play(1)\tQuit(0)" << endl; cin >> num; while (num != 0 && num != 1) { cout << "Please enter a valid input." << endl; cin >> num; } startValue = (num == 1) ? 1 : 0; } // 合并重复的游戏输入函数 void playGame(int &row, int &col) { bool validInput = false; while (!validInput) { cout << "Points: " << points << endl; cout << "Enter a coordinate:\t\tQuit(0)" << endl; cin >> row; if (row == 0) { cout << "Quitting..."; Sleep(1000); system("CLS"); exit(1); } cin >> col; if (col == 0) { cout << "Quitting..."; Sleep(1000); system("CLS"); exit(1); } if (row > chartSize || col > chartSize || row <= 0 || col <= 0) { cout << "Invalid coordinates. Please try again.\n"; } else { row--; col--; if (chart[row][col] == '*') { chart[row][col] = letters[row][col]; validInput = true; } else { cout << "This coordinate is occupied.Please enter another coordinate.\n"; } } } } void control() { if (chart[a][b] == chart[x][y]) { openCards += 2; Sleep(1000); points += 3; system("CLS"); } else { points -= 1; Sleep(1000); chart[a][b] = '*'; chart[x][y] = '*'; system("CLS"); } } int main() { mainMenu(); system("CLS"); if (startValue) { dynamicChart(); system("CLS"); while (true) { drawChart(); playGame(x, y); system("CLS"); drawChart(); playGame(a, b); system("CLS"); drawChart(); control(); if (openCards == chartSize * chartSize) { drawChart(); cout << "You won! With " << points << " points."; Sleep(3000); system("CLS"); break; } } // 释放动态分配的内存,避免泄漏 for (int i = 0; i < chartSize; ++i) { delete[] chart[i]; delete[] letters[i]; delete[] assigned[i]; } delete[] chart; delete[] letters; delete[] assigned; } else { cout << "You have quitted."; } return 0; }
关键修改点说明:
- 清理混乱指针定义:删除原全局区和函数内重复、类型不匹配的指针变量,改用
char**和int**作为动态二维数组的指针。 - 动态分配二维数组:先分配行指针数组,再为每行分配元素,程序结束前手动释放内存,避免内存泄漏。
- 简化元素访问:直接用
chart[i][j]下标方式访问元素,比多层指针解引用更直观,彻底避免类型错误。 - 优化代码冗余:合并
playGame1和playGame2为一个函数,通过引用传递参数减少重复代码。 - 补充必要头文件:添加
<ctime>确保time(NULL)能正常编译。
原错误"operand of '*' must be a pointer but has type 'int'"完全是因为指针变量重定义导致的类型冲突,修改后彻底解决了该问题,同时实现了完全的动态内存分配。
内容的提问来源于stack exchange,提问作者Mehmet
相关产品推荐
相关产品推荐

