关于指针和数组的问题
存在以下程序,其中for( p=&a[0][0], i=0; i<=6; i++, p++)改成for( p=a, i=0; i<=6; i++, p++)为什么会编译出错?编译错误信息如下:
--------------------Configuration: Test - Win32 Debug--------------------
Compiling...
CopyTest.cpp
D:\Test\CopyTest.cpp(20) : error C2440: '=' : cannot convert from 'class test [2][3]' to 'class test *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
Creating browse info file...
Test.exe - 1 error(s), 0 warning(s)
p=&a[0][0]不就是在将p指向a数组的第一个元素吗?p=a怎么就不可以呢?谢谢。
#include < iostream >
using namespace std;
class test
{
int X;
public :
test(int a)
{
X = a;
}
int GetX()
{
return X;
}
};
void main()
{
int i;
test *p, a[2][3] = {{1, 2, 3}, {4, 5, 6}};
//for( p=a, i=0; i<=6; i++, p++)为什么会编译出错?
for( p=&a[0][0], i=0; i<=6; i++, p++)
{
if((p-a[0])%3 == 0)
cout<< endl;
cout<< p->GetX() <<" ";
}
}