我創建了一個2D數組,並製作了一個指向第一個元素的指針。我試圖使用指針來打印2D數組,但是出現以下錯誤。不知道我在這裏做錯了什麼。如何通過指針打印2D數組? C++
source_file.cpp: In function ‘int main()’: source_file.cpp:15:27: error: invalid types ‘char[int]’ for array subscript cout << pname[x][y] << endl;
^
#include <iostream>
#include <string>
using namespace std;
int main()
{
char name[2][2] = {'E', 'f', 'g', 'r'};
char* pname = &name[0][0];
for (int x = 0; x<2; x++)
{
for (int y = 0; y<2; y++)
{
cout << pname[x][y] << endl;
}
}
}
你的陣列看起來像一個維 – Kad
PNAME不是一個二維數組,它是一個char * – ForeverStudent
你爲什麼要採取一個指向'name'?這可能有助於提供更實質性的答案。 – Tommy