how to allocate memory dynamically for 2 dimensional array and use that pointer like and array....
-
Hi All, I want to declare memory dynamically for a 2 dimensional array. and then want to use that pointer like an array. as i am using ANSI compiler so there is no need to typecast the return type of the memory block returned by malloc() so if i will write the inline code line as arr=malloc (nrows*5*sizeof(int)); its working correctly but i just want to know to be at the safer side that if i will be using the old compiler how to type cast the same as arr=(int [] *)(malloc (nrows*5*sizeof(int))); this is giving error.(i am on linux) thanks
#include
#include
int main()
{
int nrows=2,i,j;
//int arr[nrows][5];
int (*arr)[5];
arr=(int [] *)(malloc (nrows*5*sizeof(int)));
for(i=0;i -
Hi All, I want to declare memory dynamically for a 2 dimensional array. and then want to use that pointer like an array. as i am using ANSI compiler so there is no need to typecast the return type of the memory block returned by malloc() so if i will write the inline code line as arr=malloc (nrows*5*sizeof(int)); its working correctly but i just want to know to be at the safer side that if i will be using the old compiler how to type cast the same as arr=(int [] *)(malloc (nrows*5*sizeof(int))); this is giving error.(i am on linux) thanks
#include
#include
int main()
{
int nrows=2,i,j;
//int arr[nrows][5];
int (*arr)[5];
arr=(int [] *)(malloc (nrows*5*sizeof(int)));
for(i=0;iTry this:
arr=(int (*)[5])malloc (nrows*5*sizeof(int));
Alternatively (and this is clearer overall) use a typedef:
typedef int (*ArrType)[5];
int main()
{
ArrType arr;
arr=(ArrType)malloc (nrows*5*sizeof(int));
... Rest of code ...
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!