プログラムのコンパイル中にエラーが発生しました。コードは次のようである: -エラー2次元配列のメモリを動的に割り当てるときに、2次元配列の動的メモリ割り当てを実装しています。
# include<iostream>
# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
# define COLS 5
using namespace std;
typedef int Rowarray[COLS];
int main()
{
Rowarray *rptr;
int nrows=3;
int row,col;
rptr=(int**)malloc(nrows * COLS * sizeof(int)) ; // Error Line
for(row=0;row<nrows;row++)
{ static int i=0;
for(col=0;col <COLS;col++,i++)
{
rptr[row][col]=i;
}
}
for(row=0;row<nrows;row++)
{
for(col=0;col <COLS;col++)
{
cout << "\n" << rptr[row][col];
}
}
getch();
return 0;
}
私は取得していますエラーが `割り当てでcannot convert int**' to
のint(*)[5]」である
なぜ、 'new'ではなく' malloc'を使用していますか? – Cameron
@camron私は新しいものを使うことができますが、mallocの場合にこれらを型キャストする方法を学びたいと思っています – Invictus
'malloc(nrows * COLS * sizeof(int))'は私を強制的に変換しようとしているvoid * int **演算子の左辺はint **型です。それはエラーですか? – Invictus