Below is the part of code I am stuck on. I want to dynamically allocate memory for
- Pointer to array
- Array of pointers
I am getting several error messages like invalid conversion from int * to int and so on.
Pointer to array
int (*array)[nrows][ncolumns];
array = (int*)malloc(nrows * ncolumns * sizeof(int));
printf("\n Enter the elements:\n");
for(i=0; i<nrows; i++)
{
for(j=0; j<ncolumns; j++)
{
scanf("%d", array[i][j]);
}
}
printf("Entered array is :\n\n");
for(i = 0;i<nrows; i++)
{
for(j = 0; j<ncolumns; j++)
{
if(j== ncolumns-1)
{
printf("%d \n", *array[i][j]);
}
else
{
printf("%d", *array[i][j]);
}
Array of pointers
int *array[nrows][ncolumns];
array[nrows][ncolumns] = (int*)malloc(nrows * ncolumns * sizeof(int));
printf("Enter elements:\n");
for(i = 0; i<nrows; i++)
{
for(j = 0; j<ncolumns;j++)
{
scanf("%d",&array[i][j]);
}
}
printf("Entered array is: \n");
for(i = 0; i<nrows; i++)
{
for(j = 0; j<ncolumns;j++)
{
if(j == ncolumns-1)
{
printf("%d \n",array[i][j]);
}
else
{
printf("%d \t",array[i][j]);
}
}
}