returning array

Discussion in 'C Programming' started by shan, Nov 17, 2005.

  1. shan

    shan Guest

    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.
    shan, Nov 17, 2005
    #1
    1. Advertising

  2. "shan" <> wrote in message
    news:...
    : 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
    Ivan Vecerina, Nov 17, 2005
    #2
    1. Advertising

  3. shan

    shan Guest

    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[j]=a[j]+b[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[j]);
    }
    }
    return(a);
    }
    shan, Nov 17, 2005
    #3
  4. shan

    Chad Guest

    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[j]=a[j]+b[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[j]);


    ??

    > }
    > }
    > return(a);
    > }



    This might point you in the right direction.

    Chad
    Chad, Nov 17, 2005
    #4
  5. shan

    pete Guest

    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
    pete, Nov 17, 2005
    #5
  6. shan

    pete Guest

    pete wrote:

    > static char temp[2][4];


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

    --
    pete
    pete, Nov 17, 2005
    #6
  7. shan

    Default User Guest

    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
    Default User, Nov 17, 2005
    #7
  8. shan

    bitshadow Guest

    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.
    bitshadow, Nov 19, 2005
    #8
  9. On 17 Nov 2005 05:24:51 -0800, in comp.lang.c , "shan"
    <> 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 =----
    Mark McIntyre, Nov 19, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Fleet

    Returning reference to an array

    Andrew Fleet, Oct 25, 2003, in forum: Perl
    Replies:
    1
    Views:
    454
    Eric J. Roode
    Oct 26, 2003
  2. Mohammed Mazid
    Replies:
    2
    Views:
    1,944
    Jose Rubio
    Nov 29, 2003
  3. Ben
    Replies:
    8
    Views:
    12,618
    Eki Y. Baskoro
    Dec 18, 2003
  4. Ken
    Replies:
    3
    Views:
    9,840
    John C. Bollinger
    Jun 1, 2004
  5. Replies:
    11
    Views:
    662
    Christos Georgiou
    May 2, 2006
Loading...

Share This Page