C++ でポインター配列に相当することをするには
/*
* cpptest3.C --- ひとまずの完成
*/
#include <iostream.h>
#include <math.h>
typedef double scalar;
typedef scalar *vector;
typedef vector *matrix;
/* m行n列の行列を作る */
matrix new_matrix(int m, int n)
{
vector abody;
matrix a;
a = new vector [m];
if (a == NULL) return NULL;
abody = new scalar [m * n];
if (abody == NULL) {
delete a;
return NULL;
}
for (int i = 0; i < m; i++) {
a[i] = abody;
abody += n;
}
return a;
}
free_matrix(matrix a)
{
delete a[0];
delete a;
}
int
main()
{
int m,n,i,j;
matrix a;
cout << "m,n: "; cin >> m >> n;
a = new_matrix(m,n);
if (a == NULL) {
cerr << "new_matrix() に失敗\n";
exit(1);
}
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
a[i][j] = (i+1)*(j+1);
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++)
cout << "a[" << i << "][" << j << "]=" << a[i][j] << " ";
cout << "\n";
}
return 0;
}