By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

returning array

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
"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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
pete wrote:
static char temp[2][4];


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

--
pete
Nov 17 '05 #6

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.