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

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

How can i define arrays as 2D pointers?

问题

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

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <windows.h>
  6. #define SIZE 100
  7. using namespace std;
  8. int num, number = 1, x, y, startValue = 0, points = 0, chartSize, openCards = 0;
  9. char* ptr1 = new char[SIZE*SIZE];
  10. char* ptr2 = new char[SIZE * SIZE];
  11. int* ptr3 = new int[SIZE * SIZE];
  12. char rl, (*chart)[SIZE] = reinterpret_cast<char(*)[SIZE]>(ptr1), (*letters)[SIZE] = reinterpret_cast<char(*)[SIZE]>(ptr2);
  13. int (*assigned)[SIZE] = reinterpret_cast<int(*)[SIZE]>(ptr3);
  14. void dynamicChart() {
  15. srand(time(NULL));
  16. cout << "Please enter the size of the chart(2,4,6,etc.):" << endl;
  17. cin >> chartSize;
  18. while (chartSize % 2 != 0 || chartSize <= 0) {
  19. cout << "Invalid chart size. Please enter another value." << endl;
  20. cin >> chartSize;
  21. }
  22. int spaces = chartSize * chartSize, assigned[SIZE][SIZE] = { 0 };
  23. while (spaces != 0) {
  24. rl = 'A' + rand() % 26;
  25. int count = 0;
  26. while (count < 2) {
  27. int r1 = rand() % chartSize;
  28. int r2 = rand() % chartSize;
  29. if (!assigned[r1][r2]) {
  30. letters[r1][r2] = rl;
  31. assigned[r1][r2] = 1;
  32. count++;
  33. }
  34. }
  35. spaces -= 2;
  36. }
  37. for (int i = 0; i < chartSize; i++) {
  38. for (int j = 0; j < chartSize; j++) {
  39. chart[i][j] = '*';
  40. }
  41. }
  42. }
  43. void drawChart() {
  44. cout << "\n\n\n\n";
  45. cout << " ";
  46. for (int i = 1; i < chartSize + 1; i++) {
  47. cout << i;
  48. cout << "\t ";
  49. }
  50. cout << "\n";
  51. for (int i = 0; i < chartSize; i++) {
  52. cout << number;
  53. number++;
  54. for (int j = 0; j < chartSize; j++) {
  55. cout << "|";
  56. cout << " " << chart[i][j];
  57. cout << "\t";
  58. }
  59. cout << endl;
  60. }
  61. number = 1;
  62. }
  63. // 其余部分保持不变,只需修改数组的访问方式即可。
  64. int main() {
  65. // mainMenu() 和其他功能的调用保持不变。
  66. return 0;
  67. }

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

英文:
  1. #include &lt;iostream&gt;
  2. #include &lt;string&gt;
  3. #include &lt;stdlib.h&gt;
  4. #include &lt;windows.h&gt;
  5. #define SIZE 100
  6. using namespace std;
  7. int num, number = 1, a, b, x, y, startValue = 0, points = 0, chartSize, openCards = 0;
  8. char* ptr1 = new char[SIZE*SIZE];
  9. char* ptr2 = new char[SIZE * SIZE];
  10. int* ptr3 = new int[SIZE * SIZE];
  11. char rl, chart[SIZE][SIZE], letters[SIZE][SIZE], (*ptr1)[SIZE][SIZE] = &amp;chart, (*ptr2)[SIZE][SIZE] = &amp;letters;
  12. void dynamicChart() {
  13. srand(time(NULL));
  14. cout &lt;&lt; &quot;Please enter the size of the chart(2,4,6,etc.):&quot; &lt;&lt; endl;
  15. cin &gt;&gt; chartSize;
  16. while (chartSize % 2 != 0 || chartSize &lt;= 0) {
  17. cout &lt;&lt; &quot;Invalid chart size.Please enter another value.\n&quot;;
  18. cin &gt;&gt; chartSize;
  19. }
  20. int spaces = chartSize * chartSize, assigned[SIZE][SIZE] = { 0 },(*ptr3)[SIZE][SIZE] = &amp;assigned;
  21. while (spaces != 0) {
  22. rl = &#39;A&#39; + rand() % 26;
  23. int count = 0;
  24. while (count &lt; 2) {
  25. int r1 = rand() % chartSize;
  26. int r2 = rand() % chartSize;
  27. if (!(*(*(*ptr3 + r1) + r2))) {
  28. *(*(*ptr2 + r1) + r2) = rl;
  29. *(*(*ptr3 + r1) + r2) = 1;
  30. count++;
  31. }
  32. }
  33. spaces -= 2;
  34. }
  35. for (int i = 0; i &lt; chartSize; i++) {
  36. for (int j = 0; j &lt; chartSize; j++) {
  37. *(*(*ptr1 + i) + j) = &#39;*&#39;;
  38. }
  39. }
  40. }
  41. void drawChart() {
  42. cout &lt;&lt; &quot;\n\n\n\n&quot;;
  43. cout &lt;&lt; &quot; &quot;;
  44. for (int i = 1; i &lt; chartSize + 1; i++) {
  45. cout &lt;&lt; i;
  46. cout &lt;&lt; &quot;\t &quot;;
  47. }
  48. cout &lt;&lt; &quot;\n&quot;;
  49. for (int i = 0; i &lt; chartSize; i++) {
  50. cout &lt;&lt; number;
  51. number++;
  52. for (int j = 0; j &lt; chartSize; j++) {
  53. cout &lt;&lt; &quot;|&quot;;us
  54. cout &lt;&lt; &quot; &quot; &lt;&lt; *(*(*ptr1 + i) + j);
  55. cout &lt;&lt; &quot;\t&quot;;
  56. }
  57. cout &lt;&lt; endl;
  58. }
  59. number = 1;
  60. }
  61. void mainMenu() {
  62. cout &lt;&lt; &quot;Play(1)\tQuit(0)&quot; &lt;&lt; endl;
  63. cin &gt;&gt; num;
  64. while (num != 0 &amp;&amp; num != 1) {
  65. cout &lt;&lt; &quot;Please enter a valid input.&quot; &lt;&lt; endl;
  66. cin &gt;&gt; num;
  67. }
  68. if (num) {
  69. startValue = 1;
  70. }
  71. }
  72. void playGame1() {
  73. bool validInput = false;
  74. while (!validInput) {
  75. cout &lt;&lt; &quot;Points: &quot; &lt;&lt; points &lt;&lt; endl;
  76. cout &lt;&lt; &quot;Enter a coordinate:\t\tQuit(0)&quot; &lt;&lt; endl;
  77. cin &gt;&gt; x;
  78. if (x == 0) {
  79. cout &lt;&lt; &quot;Quitting...&quot;;
  80. Sleep(1000);
  81. system(&quot;CLS&quot;);
  82. exit(1);
  83. }
  84. cin &gt;&gt; y;
  85. if (y == 0) {
  86. cout &lt;&lt; &quot;Quitting...&quot;;
  87. Sleep(1000);
  88. system(&quot;CLS&quot;);
  89. exit(1);
  90. }
  91. if (x &gt; chartSize || y &gt; chartSize || x &lt;= 0 || y &lt;= 0) {
  92. cout &lt;&lt; &quot;Invalid coordinates. Please try again.\n&quot;;
  93. }
  94. else {
  95. x--;
  96. y--;
  97. if (*(*(*ptr1 + x) + y) == &#39;*&#39;) {
  98. *(*(*ptr1 + x) + y) = *(*(*ptr2 + x) + y);
  99. }
  100. else {
  101. cout &lt;&lt; &quot;This coordinate is occupied.Please enter another coordinate.\n&quot;;
  102. playGame1();
  103. }
  104. validInput = true;
  105. }
  106. }
  107. }
  108. void playGame2() {
  109. bool validInput = false;
  110. while (!validInput) {
  111. cout &lt;&lt; &quot;Points: &quot; &lt;&lt; points &lt;&lt; endl;
  112. cout &lt;&lt; &quot;Enter a coordinate:\t\tQuit(0)&quot; &lt;&lt; endl;
  113. cin &gt;&gt; a;
  114. if (a == 0) {
  115. cout &lt;&lt; &quot;Quitting...&quot;;
  116. Sleep(1000);
  117. system(&quot;CLS&quot;);
  118. exit(1);
  119. }
  120. cin &gt;&gt; b;
  121. if (b == 0) {
  122. cout &lt;&lt; &quot;Quitting...&quot;;
  123. Sleep(1000);
  124. system(&quot;CLS&quot;);
  125. exit(1);
  126. }
  127. if (a &gt; chartSize || b &gt; chartSize || a &lt;= 0 || b &lt;= 0) {
  128. cout &lt;&lt; &quot;Invalid coordinates. Please try again.\n&quot;;
  129. }
  130. else {
  131. a--;
  132. b--;
  133. if (*(*(*ptr1 + a) + b) == &#39;*&#39;) {
  134. *(*(*ptr1 + a) + b) = *(*(*ptr2 + a) + b);
  135. }
  136. else {
  137. cout &lt;&lt; &quot;This coordinate is occupied.Please enter another coordinate.\n&quot;;
  138. playGame2();
  139. }
  140. validInput = true;
  141. }
  142. }
  143. }
  144. void control() {
  145. if (*(*(*ptr1 + a) + b) == chart[x][y]) {
  146. openCards += 2;
  147. Sleep(1000);
  148. points += 3;
  149. system(&quot;CLS&quot;);
  150. }
  151. else {
  152. points -= 1;
  153. Sleep(1000);
  154. *(*(*ptr1 + a) + b) = chart[x][y] = &#39;*&#39;;
  155. system(&quot;CLS&quot;);
  156. }
  157. }
  158. int main() {
  159. mainMenu();
  160. system(&quot;CLS&quot;);
  161. if (startValue) {
  162. dynamicChart();
  163. system(&quot;CLS&quot;);
  164. while (1) {
  165. drawChart();
  166. playGame1();
  167. system(&quot;CLS&quot;);
  168. drawChart();
  169. playGame2();
  170. system(&quot;CLS&quot;);
  171. drawChart();
  172. control();
  173. if (openCards == chartSize * chartSize) {
  174. drawChart();
  175. cout &lt;&lt; &quot;You won! With &quot; &lt;&lt; points &lt;&lt; &quot; points.&quot;;
  176. Sleep(3000);
  177. system(&quot;CLS&quot;);
  178. break;
  179. }
  180. }
  181. }
  182. else {
  183. cout &lt;&lt; &quot;You have quitted.&quot;;
  184. }
  185. return 0;
  186. }

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

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

  1. // 分配2D数组
  2. char** ptr1 = new char*[chartSize];
  3. for (int i = 0; i < chartSize; i++) {
  4. ptr1[i] = new char[chartSize];
  5. }
  6. // 初始化2D数组元素
  7. for (int i = 0; i < chartSize; i++) {
  8. for (int j = 0; j < chartSize; j++) {
  9. ptr1[i][j] = '*';
  10. }
  11. }
  12. 一旦你分配了数组,其他部分(几乎)与常规数组完全相同。
英文:

Here's a quick sample to get you started

  1. // allocate 2D array
  2. char** ptr1 = new char*[chartSize];
  3. for (int i = 0; i &lt; chartSize; i++) {
  4. ptr1[i] = new char[chartSize];
  5. // initialise 2D array elements
  6. for (int i = 0; i &lt; chartSize; i++) {
  7. for (int j = 0; j &lt; chartSize; j++) {
  8. ptr1[i][j] = &#39;*&#39;;
  9. }
  10. }

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:

确定