473,657 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(size of(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.1L f",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*size of(long double *));

for(i=0;i<m;i++ )
a[i]=malloc(m*sizeo f(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 2319
On 16 Sep 2005 03:42:41 -0700, "Guha" <su********@gma il.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(size of(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.1L f",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*size of(long double *));

for(i=0;i<m;i++ )
a[i]=malloc(m*sizeo f(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
14016
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 value of a function acts like 'by Val' returning the value only (except for objects) can you make it return a reference instead? cheers, Krackers
7
7292
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 array e.g: array *a; a = function(...)
5
3086
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 function below is returning a pointer to pointers who are GUID. I am trying to write a wrapper to use in my VB code and what I would prefer to do is be able to return an array of GUID. I remember (not sure) that the concept of arrays does not really...
41
3801
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 = rand(); y = rand();
10
3155
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 in levels of indirection, so I feel it must have something to do with the way I am representing the array in the call and the return. Below I have commented the problem parts. Thanks in advance for any help offered. Pete
2
7141
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
17
3240
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: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
13
2545
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 data from a query into an array. The intent is that the following code:
0
4081
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 return me Array element type code 1, but it is returning me type code 12 and the array in a junk or unreadable format . Our environment is JDK 1.4, ojdbc14.jar and Oracle 8i Not able to find out what they problem is and am not able to change the...
5
2675
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 that structure, is this valid? As shown in the code below I am allocating the structure in the function and then returning the structure. I know if the structure contained only simple types (int, float) this will work without problems as you...
0
8402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8734
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8608
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2733
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.