这段代码有什么问题?它在运行时显示内存转储错误
#include<iostream>
using namespace std ;
int main()
{
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
int **p = A;
P[1][2] = 99;
cout<<A[1][2] ;
}
这段代码有什么问题?它在运行时显示内存转储错误
#include<iostream>
using namespace std ;
int main()
{
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
int **p = A;
P[1][2] = 99;
cout<<A[1][2] ;
}
将您的更改int **p = A[0][0]
为int *p = &A[0][0]
. 在下一行中,编写以下内容*p = *((int*)p + 1 * NUM_OF_COLUMNS + 2) = 99;
,其中 NUM_OF_COLUMNS 是数字 4,而不是P[1][2] = 99;
. 更正主要的拼写以及变量的大写/小写。return 0;
还要在末尾添加 a ,因为您有 aint main()
而不是 void。
您似乎是 C++ 新手或遇到类似问题的编程不会感觉不好,因为指针可能很棘手,如果您不知道您不知道。我很确定这会对你有所帮助。记得选择最佳答案:)。
#include <iostream>
using namespace std;
int main() {
int A[3][4] = { { 3, 1, 8, 11 }, { 4, 12, 9, 10 }, { 7, 5, 2, 6 } };
cout << "Before pointer change A[1][2] = " << A[1][2] << endl;
int *p; //Set pointer
p = &A[1][2]; //Set memory address to pointer don't forget '&'
*p = 99; //Change integer
cout << "After pointer change A[1][2] = " << A[1][2] << endl;
return 0; // you need a 'return 0;' because your main is int
}