`Dear friends, I was trying to define a 2-D array named A using a dummy variable m through malloc.The variable A has been initialized to 0. It is expected that new values get assigned to the matrix after the run .But that doesn't happen here.I would like to understand the correct way of doing this.I also tried method described in dynamic memory allocation in 2d array and using scanf and also avoided casting malloc. Here is the code
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
double** Allocate(int d1,int d2);
double** Allocate(int d1,int d2)
{
double **m;
int i,j;
m = (double**)malloc(sizeof(double*)*d1);
for(i=0;i<d1;i++)
{
m[i]=(double*)malloc(sizeof(double)*d2);
}
for(i=0;i<d1;i++)
{
for(j=0;j<d2;j++)
{
m[i][j]=0.0;
}
}
return m;
}
main()
{
int i,j;
int n=3;
float x[n],y[n];
double** A= Allocate(n,n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
scanf("%f",&A[i][j]);
}
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%f\t",A[i][j]);
}printf("\n");
}
}