G
Guha
I have a problem with returning a 2D array using a function which is
called in main(). The piece of the code is given below. This is a test
code only.
#include"stdio.h"
#include"alloc.h"
void main()
{
long double **mat;
int i,j,n;
long double **test(int m);
clrscr();
printf("\nEnter The Value of n\n");
scanf("%d",&n);
/* allocating the memory of mat */
mat=malloc(sizeof(long double *)*n);
for(i=0;i<n;i++)
mat=malloc(sizeof(long double )*n);
/* base address of **a is stored in mat */
mat=test(n);
/* Printing the values of 2D array */
for(i=0;i<n;i++)
{
printf("");
for(j=0;j<n;j++)
{
printf("\n%3.1Lf",mat[j]);
}
}
for(i=0;i<n;i++)
free(mat);
free(mat);
getch();
}
long double **test(int m)
{
int i,j;
long double **a;
a=malloc(m*sizeof(long double *));
for(i=0;i<m;i++)
a=malloc(m*sizeof(long double));
for(i=0;i<m;i++)
for(j=0;j<m;j++)
a[j]=i; /* assiging some values */
return(a);
}
1) using the function **test() I'm able to return the base address of
the 2D array **a and it also prints the results correctly while n<56
but the problem is when the value of n goes beyond 56 it prints ascii
characters in the output.
2) another problem is that I'm not able to free the memeory allocated
for the variable **a because if I add any line after the return()
command the lines are unreachable and if I had to free the memory
before returning the varibale address it would return nothing.
Please suggest what is to be done.
Thnaks.
called in main(). The piece of the code is given below. This is a test
code only.
#include"stdio.h"
#include"alloc.h"
void main()
{
long double **mat;
int i,j,n;
long double **test(int m);
clrscr();
printf("\nEnter The Value of n\n");
scanf("%d",&n);
/* allocating the memory of mat */
mat=malloc(sizeof(long double *)*n);
for(i=0;i<n;i++)
mat=malloc(sizeof(long double )*n);
/* base address of **a is stored in mat */
mat=test(n);
/* Printing the values of 2D array */
for(i=0;i<n;i++)
{
printf("");
for(j=0;j<n;j++)
{
printf("\n%3.1Lf",mat[j]);
}
}
for(i=0;i<n;i++)
free(mat);
free(mat);
getch();
}
long double **test(int m)
{
int i,j;
long double **a;
a=malloc(m*sizeof(long double *));
for(i=0;i<m;i++)
a=malloc(m*sizeof(long double));
for(i=0;i<m;i++)
for(j=0;j<m;j++)
a[j]=i; /* assiging some values */
return(a);
}
1) using the function **test() I'm able to return the base address of
the 2D array **a and it also prints the results correctly while n<56
but the problem is when the value of n goes beyond 56 it prints ascii
characters in the output.
2) another problem is that I'm not able to free the memeory allocated
for the variable **a because if I add any line after the return()
command the lines are unreachable and if I had to free the memory
before returning the varibale address it would return nothing.
Please suggest what is to be done.
Thnaks.