473,394 Members | 1,658 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,394 software developers and data experts.

returning array

How to return an two dimensional array in user defined function to main
function.
I think it is posible by using pointers but don't know how to code.or
is there any method without using pointers.It is just a queston arised
by me not homework,I specify this because in my previous posts I get
replied by such comments.

Nov 17 '05 #1
8 1858
"shan" <sr**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
: How to return an two dimensional array in user defined function to main
: function.
: I think it is posible by using pointers but don't know how to code.or
: is there any method without using pointers.It is just a queston arised
: by me not homework,I specify this because in my previous posts I get
: replied by such comments.

If the array has a fixed size (e.g. a 2D matrix), the easiest is
to wrap in in a struct:
struct Matrix2D {
double v[2][2];
};
struct Matrix2D f(); // easy function declaration

If the array can have a variable size, you need to return a pointer
to dynamically allocated memory. Maybe look for posts about returning
strings (a character array), to see what the options are.
You can either flatten the 2D array into a linear array, or return a
pointer to a heap-allocated array of pointer to heap-allocated arrays.
hth --Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Nov 17 '05 #2

shan wrote:
How to return an two dimensional array in user defined function to main
function.
I think it is posible by using pointers but don't know how to code.or
is there any method without using pointers.It is just a queston arised
by me not homework,I specify this because in my previous posts I get
replied by such comments.

The above question arises
due to the following program.
The following program is to find matrix addition.Get matrix elements
by a function known as enter().it should be called twice from main.At
first time the array variable should be 'a' but at the second time it
should be changed to 'b' or some else.How can it be done.

#include<stdio.h>
int a[3][3],rows,cols;
main()
{
int mat_add[3][3];
int enter(void);
enter();
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
mat_add[i][j]=a[i][j]+b[i][j];
printf("%d",mat_add);
}
int enter( void)
{
int i,j;
printf("Enter how many rows to be entered\n");
scanf("%d",&rows);
printf("Enter how many columns to be entered\n");
scanf("%d",&cols);
printf("Enter the matrix in the form\n1 0 1\n9 8 7\n7 5 2\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols);j++)
{
if ('\n'==getchar())
break;
else
scanf("%d",a[i][j]);
}
}
return(a);
}

Nov 17 '05 #3
shan wrote:
shan wrote:
How to return an two dimensional array in user defined function to main
function.
I think it is posible by using pointers but don't know how to code.or
is there any method without using pointers.It is just a queston arised
by me not homework,I specify this because in my previous posts I get
replied by such comments. The above question arises
due to the following program.
The following program is to find matrix addition.Get matrix elements
by a function known as enter().it should be called twice from main.At
first time the array variable should be 'a' but at the second time it
should be changed to 'b' or some else.How can it be done.

#include<stdio.h>
int a[3][3],rows,cols;
main()


This should be int main(void);
{
int mat_add[3][3];
int enter(void);
enter();
Why do you call enter() twice?
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
You don't tell us what the value of rows and cols are. As the regulars
on this forum would say. Undefined behavior will ensue.
mat_add[i][j]=a[i][j]+b[i][j];
printf("%d",mat_add);
}
int enter( void)
{
int i,j;
printf("Enter how many rows to be entered\n");
scanf("%d",&rows);
You should check too see if scanf() fails.
printf("Enter how many columns to be entered\n");
scanf("%d",&cols);
Ditto.
printf("Enter the matrix in the form\n1 0 1\n9 8 7\n7 5 2\n");
for(i=0;i<rows;i++)
You don't specificy what the value of rows should be.
{
for(j=0;j<cols);j++)
On the same note, you don't specify the number of columns.

For rows and columns, either pass rows and col as additonal arguments
or rewrite int add[3][3] as like

#define ROWS 3
#define COLS 3

add[ROW][COLS];
{
if ('\n'==getchar())
I'm pretty sure '\n'==getchar() is illegal. Reverse it. Also, why do
you need to getchar() when scan() gets the variables?
break;
I think this is only used when you use a switch statement. YOu can omit
this and it won't have any impact on this code.
else
scanf("%d",a[i][j]);
??
}
}
return(a);
}

This might point you in the right direction.

Chad

Nov 17 '05 #4
shan wrote:

How to return an two dimensional array in
user defined function to main
function.
I think it is posible by using pointers but don't know how to code.


/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

char (*X(char (*array)[4]))[4];

int main(void)
{
char array[][4] = {"one", "two"};
char (*ptr)[4];

ptr = array;
puts(ptr[0]);
puts(ptr[1]);
putchar('\n');
ptr = X(array);
puts(ptr[0]);
puts(ptr[1]);
return 0;
}

char (*X(char (*array)[4]))[4]
{
static char temp[2][4];

strcpy(temp[1], array[0]);
strcpy(temp[0], array[1]);
return temp;
}

/* END new.c */

--
pete
Nov 17 '05 #5
pete wrote:
static char temp[2][4];


"temp" is a poor choice to name an object with static duration.

--
pete
Nov 17 '05 #6
Chad wrote:
shan wrote:
shan wrote:
How to return an two dimensional array in user defined function
to main function.
I think it is posible by using pointers but don't know how to
code.or is there any method without using pointers.It is just a
queston arised by me not homework,I specify this because in my
previous posts I get replied by such comments. The above question
arises due to the following program.
The following program is to find matrix addition.Get matrix
elements by a function known as enter().it should be called twice
from main.At first time the array variable should be 'a' but at the
second time it should be changed to 'b' or some else.How can it be
done.

#include<stdio.h>
int a[3][3],rows,cols;
main()


This should be int main(void);
{
int mat_add[3][3];
int enter(void);
enter();


Why do you call enter() twice?


He doesn't. The first is a declaration.

for(i=0;i<rows;i++)
for(j=0;j<cols;j++)


You don't tell us what the value of rows and cols are. As the regulars
on this forum would say. Undefined behavior will ensue.


Those are global variables that are set during the call to enter().
printf("Enter the matrix in the form\n1 0 1\n9 8 7\n7 5 2\n");
for(i=0;i<rows;i++)


You don't specificy what the value of rows should be.


Again, yes he does.
For rows and columns, either pass rows and col as additonal arguments
or rewrite int add[3][3] as like

#define ROWS 3
#define COLS 3

add[ROW][COLS];
Or use global variables. That's probably the worst available choice,
but not incorrect.
{
if ('\n'==getchar())


I'm pretty sure '\n'==getchar() is illegal. Reverse it.


Why do you think that?
Also, why do
you need to getchar() when scan() gets the variables?
That's just it, it gets the int but leaves behind the carriage return.
break;


I think this is only used when you use a switch statement.


I think you're wrong. It's also used to exit loops, like that inner for
loop.
YOu can
omit this and it won't have any impact on this code.
No, it won't. It will leave behind the newlines and create havoc (I
think).

This might point you in the right direction.

Urg. I applaud your attempt to help, but it looks like you have a lot
to learn yourself. I suspect you're not at the advice dispensing stage
just yet.

Brian
Nov 17 '05 #7
shan wrote:
How to return an two dimensional array in user defined function to main
function.
or
is there any method without using pointers.


aside from wraping it into a struct, i believe the real easiar way is
to make it static. ex:
static int (*array)[COLS]
since your array is an automatic varible and subsequently created on
the "stack" on return from the funtion the address is lost but if you
make it static it stays for the duration of your program.

Nov 19 '05 #8
On 17 Nov 2005 05:24:51 -0800, in comp.lang.c , "shan"
<sr**********@gmail.com> wrote:

shan wrote:
How to return an two dimensional array in user defined function to main
function.

a) pass the array as a parameter to the function
int enter(int array[12][12]);

b) encapsulate the array in a struct, and return the struct
struct {array[12][12] } thearray;
struct thearray enter(void);

Both methods require you do define a max size for the array, or
alternatively use malloc to create some dynamic memory.

By the way, I would also pass pointers to rows and cols to enter(), so
that you don't need to define these globally.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 19 '05 #9

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...
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
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...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
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...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.