how to allocate memory for a two dimensional array dynamically please need code for that one

how to allocate memory for a two dimensional array dynamically please need code for that one

you need a pointer to pointer like

int **myArray;

then, first allocate for the int ** like

myArray = (int**)malloc(sizeof(int*) * HOW_MANY);

and then for the int * , for each row in the column, you need to allocate memory again, perhaps in a for loop.

for (i = 0; i < HOW_MANY; i++)
{
     myArray[i] = (int *)malloc(HOW_MANY_ROW * sizeof(int));
}

now, your array is created on the runtime, dynamically

I am sure this helps...

May be this can help

void main()
{
int n,i,*p;
printf("\nHow many elements\n");
scanf("%d",&n);
p=(int *)malloc(sizeof(int));
printf("\nEnter %d Elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&p);
}

Now uve alloted memory for an array dynamically and stored elements into the dynamic array

This article has been dead for over six months. Start a new discussion instead.