0

`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");
  }
 }
6
  • 3
    Don't cast the result of malloc, and don't forget to check it for NULL. Commented Jul 20, 2016 at 16:06
  • 4
    scanf("%f",&A[i][j]); use %lffor double --> scanf("%lf",&A[i][j]); Commented Jul 20, 2016 at 16:10
  • Possibly a better way is to have double Array[d1][d2]; followed by *double *ArrayPointer=malloc(d1*d2*sizeof(double)); * , and proceed from there. Commented Jul 20, 2016 at 16:12
  • @JonathonReinhart on modern systems, checking it for null guarantees nothingin particular. Commented Jul 20, 2016 at 16:35
  • @n.m. Care to elaborate? Are you suggesting we stop checking malloc result for NULL? Commented Jul 20, 2016 at 17:35

1 Answer 1

1
  • You are using the wrong format specifier for scanning a double type variable here :

    scanf("%f",&A[i][j]);
            ^ 
    
  • The correct format specifier is %lf, so instead scan this way into the array :

    scanf("%lf",&A[i][j]);
    

  • You need not use %lf while using printf() because as @Robᵩ suggested in the comment :

in printf(), %f is identical to %lf. Both accept a double

to know more, see this : click


  • further, don't use main() as it is not a valid signature for main(), instead use int main(void),because in standard C, the only valid signatures for main are:

    int main(void) //when arguments are not required
    
  • And also,

    int main(int argc, char **argv) //when you require to send arguments

to know more, see this : click


  • And always make sure to free the malloced memory at the end

      ..........  
         printf("%lf\t",A[i][j]);
        }printf("\n");
      }
    
      //freeing the malloced memory
      for(i=0; i<n; i++)
      {
          free(A[i]);
      }
      free(A);
    
    }//end of main
    

Making above changes, your main() function would be :

int main(void)
{
    int i,j;
    int n=3;

    double** A= Allocate(n,n);

    for(i=0;i<=n-1;i++)
    {
        for(j=0;j<=n-1;j++)
        {
            scanf("%lf",&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");
     }

      for(i=0; i<n; i++)
      {
          free(A[i]);
      }
      free(A);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

You are absolutely correct w.r.t. scanf(). But, in printf(), %f is identical to %lf. Both accept a double.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.