你如何将数组定义为二维指针?

huangapple go评论89阅读模式
英文:

How can i define arrays as 2D pointers?

问题

你需要将这些数组转换为指针并使用动态内存分配。以下是已经进行转换的代码段:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <windows.h>

#define SIZE 100

using namespace std;

int num, number = 1, x, y, startValue = 0, points = 0, chartSize, openCards = 0;

char* ptr1 = new char[SIZE*SIZE];
char* ptr2 = new char[SIZE * SIZE];
int* ptr3 = new int[SIZE * SIZE];

char rl, (*chart)[SIZE] = reinterpret_cast<char(*)[SIZE]>(ptr1), (*letters)[SIZE] = reinterpret_cast<char(*)[SIZE]>(ptr2);
int (*assigned)[SIZE] = reinterpret_cast<int(*)[SIZE]>(ptr3);

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." << endl;
        cin >> chartSize;
    }
    int spaces = chartSize * chartSize, assigned[SIZE][SIZE] = { 0 };

    while (spaces != 0) {
        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;
        cout << "\t    ";
    }

    cout << "\n";

    for (int i = 0; i < chartSize; i++) {
        cout << number;
        number++;
        for (int j = 0; j < chartSize; j++) {
            cout << "|";
            cout << "   " << chart[i][j];
            cout << "\t";
        }
        cout << endl;
    }

    number = 1;
}

// 其余部分保持不变,只需修改数组的访问方式即可。

int main() {
    // mainMenu() 和其他功能的调用保持不变。
    return 0;
}

这些更改将数组转换为指针,使得您可以使用动态内存分配。请注意,我使用了 reinterpret_cast 来将指针类型转换为正确的数组类型。您需要根据这个示例来修改其他部分,以便使用新的指针而不是数组。希望这可以帮助您解决问题!如果您需要进一步的帮助,请随时提问。

英文:
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;stdlib.h&gt;
#include &lt;windows.h&gt;
#define SIZE 100
using namespace std;
int num, number = 1, a, b, x, y, startValue = 0, points = 0, chartSize, openCards = 0;
char* ptr1 = new char[SIZE*SIZE];
char* ptr2 = new char[SIZE * SIZE];
int* ptr3 = new int[SIZE * SIZE];
char rl, chart[SIZE][SIZE], letters[SIZE][SIZE], (*ptr1)[SIZE][SIZE] = &amp;chart, (*ptr2)[SIZE][SIZE] = &amp;letters;
void dynamicChart() {
srand(time(NULL));
cout &lt;&lt; &quot;Please enter the size of the chart(2,4,6,etc.):&quot; &lt;&lt; endl;
cin &gt;&gt; chartSize;
while (chartSize % 2 != 0 || chartSize &lt;= 0) {
cout &lt;&lt; &quot;Invalid chart size.Please enter another value.\n&quot;;
cin &gt;&gt; chartSize;
}
int spaces = chartSize * chartSize, assigned[SIZE][SIZE] = { 0 },(*ptr3)[SIZE][SIZE] = &amp;assigned;
while (spaces != 0) {
rl = &#39;A&#39; + rand() % 26;
int count = 0;
while (count &lt; 2) {
int r1 = rand() % chartSize;
int r2 = rand() % chartSize;
if (!(*(*(*ptr3 + r1) + r2))) {
*(*(*ptr2 + r1) + r2) = rl;
*(*(*ptr3 + r1) + r2) = 1;
count++;
}
}
spaces -= 2;
}
for (int i = 0; i &lt; chartSize; i++) {
for (int j = 0; j &lt; chartSize; j++) {
*(*(*ptr1 + i) + j) = &#39;*&#39;;
}
}
}
void drawChart() {
cout &lt;&lt; &quot;\n\n\n\n&quot;;
cout &lt;&lt; &quot;     &quot;;
for (int i = 1; i &lt; chartSize + 1; i++) {
cout &lt;&lt; i;
cout &lt;&lt; &quot;\t    &quot;;
}
cout &lt;&lt; &quot;\n&quot;;
for (int i = 0; i &lt; chartSize; i++) {
cout &lt;&lt; number;
number++;
for (int j = 0; j &lt; chartSize; j++) {
cout &lt;&lt; &quot;|&quot;;us
cout &lt;&lt; &quot;   &quot; &lt;&lt;  *(*(*ptr1 + i) + j);
cout &lt;&lt; &quot;\t&quot;;
}
cout &lt;&lt; endl;
}
number = 1;
}
void mainMenu() {
cout &lt;&lt; &quot;Play(1)\tQuit(0)&quot; &lt;&lt; endl;
cin &gt;&gt; num;
while (num != 0 &amp;&amp; num != 1) {
cout &lt;&lt; &quot;Please enter a valid input.&quot; &lt;&lt; endl;
cin &gt;&gt; num;
}
if (num) {
startValue = 1;
}
}
void playGame1() {
bool validInput = false;
while (!validInput) {
cout &lt;&lt; &quot;Points: &quot; &lt;&lt; points &lt;&lt; endl;
cout &lt;&lt; &quot;Enter a coordinate:\t\tQuit(0)&quot; &lt;&lt; endl;
cin &gt;&gt; x;
if (x == 0) {
cout &lt;&lt; &quot;Quitting...&quot;;
Sleep(1000);
system(&quot;CLS&quot;);
exit(1);
}
cin &gt;&gt; y;
if (y == 0) {
cout &lt;&lt; &quot;Quitting...&quot;;
Sleep(1000);
system(&quot;CLS&quot;);
exit(1);
}
if (x &gt; chartSize || y &gt; chartSize || x &lt;= 0 || y &lt;= 0) {
cout &lt;&lt; &quot;Invalid coordinates. Please try again.\n&quot;;
}
else {
x--;
y--;
if (*(*(*ptr1 + x) + y) == &#39;*&#39;) {
*(*(*ptr1 + x) + y) = *(*(*ptr2 + x) + y);
}
else {
cout &lt;&lt; &quot;This coordinate is occupied.Please enter another coordinate.\n&quot;;
playGame1();
}
validInput = true;
}
}
}
void playGame2() {
bool validInput = false;
while (!validInput) {
cout &lt;&lt; &quot;Points: &quot; &lt;&lt; points &lt;&lt; endl;
cout &lt;&lt; &quot;Enter a coordinate:\t\tQuit(0)&quot; &lt;&lt; endl;
cin &gt;&gt; a;
if (a == 0) {
cout &lt;&lt; &quot;Quitting...&quot;;
Sleep(1000);
system(&quot;CLS&quot;);
exit(1);
}
cin &gt;&gt; b;
if (b == 0) {
cout &lt;&lt; &quot;Quitting...&quot;;
Sleep(1000);
system(&quot;CLS&quot;);
exit(1);
}
if (a &gt; chartSize || b &gt; chartSize || a &lt;= 0 || b &lt;= 0) {
cout &lt;&lt; &quot;Invalid coordinates. Please try again.\n&quot;;
}
else {
a--;
b--;
if (*(*(*ptr1 + a) + b) == &#39;*&#39;) {
*(*(*ptr1 + a) + b) = *(*(*ptr2 + a) + b);
}
else {
cout &lt;&lt; &quot;This coordinate is occupied.Please enter another coordinate.\n&quot;;
playGame2();
}
validInput = true;
}
}
}
void control() {
if (*(*(*ptr1 + a) + b) == chart[x][y]) {
openCards += 2;
Sleep(1000);
points += 3;
system(&quot;CLS&quot;);
}
else {
points -= 1;
Sleep(1000);
*(*(*ptr1 + a) + b) = chart[x][y] = &#39;*&#39;;
system(&quot;CLS&quot;);
}
}
int main() {
mainMenu();
system(&quot;CLS&quot;);
if (startValue) {
dynamicChart();
system(&quot;CLS&quot;);
while (1) {
drawChart();
playGame1();
system(&quot;CLS&quot;);
drawChart();
playGame2();
system(&quot;CLS&quot;);
drawChart();
control();
if (openCards == chartSize * chartSize) {
drawChart();
cout &lt;&lt; &quot;You won! With &quot; &lt;&lt; points &lt;&lt; &quot; points.&quot;;
Sleep(3000);
system(&quot;CLS&quot;);
break;
}
}
}
else {
cout &lt;&lt; &quot;You have quitted.&quot;;
}
return 0;
}

This is a memory game that i am working on. I need to change the arrays to pointers and use dynamic memory allocation but i am not beeing able to do it. I am having error message "operand of '*' must be a pointer but has type 'int'." whenever i try to use my pointers.

I tried to use pointer to pointer but it didn't worked out.Instead i had many more errors.

答案1

得分: -2

以下是翻译好的代码部分:

// 分配2D数组
char** ptr1 = new char*[chartSize];
for (int i = 0; i < chartSize; i++) {
    ptr1[i] = new char[chartSize];
}

// 初始化2D数组元素
for (int i = 0; i < chartSize; i++) {
    for (int j = 0; j < chartSize; j++) {
        ptr1[i][j] = '*';
    }
}

一旦你分配了数组,其他部分(几乎)与常规数组完全相同。
英文:

Here's a quick sample to get you started

// allocate 2D array
char** ptr1 = new char*[chartSize];
for (int i = 0; i &lt; chartSize; i++) {
ptr1[i] = new char[chartSize];
// initialise 2D array elements
for (int i = 0; i &lt; chartSize; i++) {
for (int j = 0; j &lt; chartSize; j++) {
ptr1[i][j] = &#39;*&#39;;
}
}

Once you have the arrays allocated everything else is (almost) exactly the same as regular arrays.

huangapple
  • 本文由 发表于 2023年5月7日 21:52:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定