//动态申请3*4二维数组空间
//In C
int main()
{
//1.转换为int**
int** p = (int**)malloc(3*sizeof(int*)); //p为一个行指针**,指向一个一维数组的地址
for(int i = 0; i < 3; i++)
{
p[i] = (int*)malloc(4*sizeof(int)); //p[i]为一个列指针
}
int num = 0;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 4; j++)
{
*(*(p + i) + j) = num++;
}
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
cout << *(*(p + i) + j) << " ";
}
cout << endl;
}
for(int i = 0; i < 3; i++)
{
free(p[i]);
}
//2.转换为int*
int* p = (int*)malloc(3 * 4 * sizeof(int));
for(int i = 0; i < 12; i++)
{
*(p + i) = i;
}
for(int i = 0; i < 12; i++)
{
cout << *(p + i) << " ";
if((i + 1)%4 == 0)
{
cout << endl;
}
}*/
free(p);
cout << "All Done!" << endl;
getchar();
return 1;
}
//In C++
int main()
{
int (*p)[4] = new int[3][4];
int num = 0;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 4; j++)
{
*(*(p + i) + j) = num++;
}
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
cout << *(*(p + i) + j) << " ";
}
cout << endl;
}
delete[] p;
cout << "All Done!" << endl;
getchar();
return 1;
}