473,378 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Returning a 2D array from a function

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[i]=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[i][j]);
}
}
for(i=0;i<n;i++)
free(mat[i]);

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[i]=malloc(m*sizeof(long double));

for(i=0;i<m;i++)
for(j=0;j<m;j++)
a[i][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.

Nov 15 '05 #1
1 2300
On 16 Sep 2005 03:42:41 -0700, "Guha" <su********@gmail.com> wrote:
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"
This is a standard header. Use <stdio.h>
#include"alloc.h"
No such header. Use <stdlib.h>

void main()
int main(void) please
{

long double **mat;
int i,j,n;

long double **test(int m);
clrscr();
A nonstandard function you don't need.

printf("\nEnter The Value of n\n");
scanf("%d",&n);
/* allocating the memory of mat */
mat=malloc(sizeof(long double *)*n);
You should always check malloc for success.

for(i=0;i<n;i++)
mat[i]=malloc(sizeof(long double )*n);

/* base address of **a is stored in mat */
mat=test(n);
This creates a memory leak. You no longer know the addresses returned
by any of the previous n+1 calls to malloc.

On the other hand, since you want to allocate the memory in test, you
should not be doing any calls to malloc in main.

/* Printing the values of 2D array */
for(i=0;i<n;i++)
{
printf("");
And this accomplishes what?
for(j=0;j<n;j++)
{
printf("\n%3.1Lf",mat[i][j]);
}
}
for(i=0;i<n;i++)
free(mat[i]);
This is freeing the memory allocated in test. The memory allocated in
main is permanently leaked.

free(mat);
getch();
Another nonstandard function you don't need. Use getchar().
}

long double **test(int m)
{
int i,j;

long double **a;

a=malloc(m*sizeof(long double *));

for(i=0;i<m;i++)
a[i]=malloc(m*sizeof(long double));

for(i=0;i<m;i++)
for(j=0;j<m;j++)
a[i][j]=i; /* assiging some values */
return(a);
return is not a function but a statement. You don't need the
parentheses.

}
1) using the function **test() I'm able to return the base address of
The function name is test. The ** is part of the return type, not
part of the name.
the 2D array **a and it also prints the results correctly while n<56
The pointer (not array) name is a. The ** is part of the type...
but the problem is when the value of n goes beyond 56 it prints ascii
characters in the output.
Sounds like malloc is failing and you then invoke undefined behavior.

All of your output is ascii characters. However, it is really strange
for a %Lf format to produce recognizable "character" output as opposed
to "numerical" 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.
No you don't. You can free the memory in main since main now knows
where the memory allocated by test is.

Please suggest what is to be done.
Thnaks.

<<Remove the del for email>>
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
7
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
2
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
0
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.