I am implementing a two dimensional array dynamic memory allocation . While compiling the program i got an error . The code is as follows :-
# 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;
}
The error i am getting is cannot convert int**' toint (*)[5]’ in assignment `
Try removing the cast (parentheses) to the left of malloc. If that doesn’t work replace them with
(Rowarray *).