Thread: Returning Multi dimension array

  1. #1
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118

    Returning Multi dimension array

    I am a beginner of C language. I am confused, how to return a multi dimentional array from function!
    Code:
     #include<stdio.h>
    #include<conio.h>
    #define n 2
    
    int * add(int [n][n],int [n][n]);
    
    main()
    {
        int c[n][n];
        int a[n][n]={ {1,2},{3,4} };
        int b[n][n]={ {4,5},{13,4} };
        int i,j;
    
        c=multi(a[n][n],b[n][n]);
    
        for(i=0;i<n;i++)
        {
            printf("\n");
            for(j=0;j<n;j++)
            printf("%5d",c[i][j]);
        }
    }
    
    int * add(int a[n][n],int b[n][n])
    {
        int c[n][n];
        int i,j;
    
        for(i=0;i<n;i++)
        for(j=0;j<n;j++)
        c[i][j]=a[i][j]+b[i][j];
    
        return(c);
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Returning the array name c is fine as long as the caller can handle a pointer to an array of integers.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You can also have the user pass in a pointer to an array as one of the parameters, and then your function uses that pointer to write the result. It seems counter-intuitive, but that way the caller is entirely in control of allocating and deallocating memory, etc... This is quite common in libraries, because it leaves the return value open for error codes, etc...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No one bothered to mention that c is not an int *, and that you can't return a pointer to a local variable here I see.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No one bothered to mention that a 2D array does not decay to a pointer either.
    The arrays you pass into the function are already passed by address. Use that to your advantage. Use it to pass in an array to fill up so the caller can acquire the information.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Quote Originally Posted by itCbitC View Post
    Returning the array name c is fine as long as the caller can handle a pointer to an array of integers.
    Yes i have tried it this way, but it is still not working and giving an error as

    can not convert int to int *

    my code was like this
    [code]

    int *c;
    .
    .
    .
    .
    c=add(a[][],b[][]);

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First off...
    c=add(a[][],b[][]);
    ...is not a valid function call.
    It should be...
    c=add(a,b);
    ...to pass the 2D arrays to the function.

    Secondly, as we've told you: a 2D array isn't a pointer. You cannot assign a 2D array to mere pointer. Don't attempt it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Quote Originally Posted by Elysia View Post
    No one bothered to mention that a 2D array does not decay to a pointer either.
    The arrays you pass into the function are already passed by address. Use that to your advantage. Use it to pass in an array to fill up so the caller can acquire the information.
    yes that's the only way i haven't tried yet.

    thank for your help and support.

  9. #9
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Quote Originally Posted by Elysia View Post
    First off...
    c=add(a[][],b[][]);
    ...is not a valid function call.
    It should be...
    c=add(a,b);
    ...to pass the 2D arrays to the function.

    Secondly, as we've told you: a 2D array isn't a pointer. You cannot assign a 2D array to mere pointer. Don't attempt it.
    You mean that as i can use name of a single dimention array as it's base address but name of two dimention array can't be used as address!
    I mean in the following declaration
    int array1[]

    "array1" is the base address of arrray but in declaration

    int array2[][]

    "array2" is not!

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it can. However, its type is different:

    int myarr[10];
    int * pArr = myarr; // OK

    int myarr[5][10];
    int * pArr = myarr; // Not OK
    int (*pArr)[10] = myarr; // OK
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Use all upper case for #define macro.
    #define N 2
    Or use enum.
    Array decays to pointer.
    2D array decays to pointer to array of element COLUMN . (i.e (*T)[COL] ).
    E.g
    Code:
    int (*add(void))[ N ] 
    {
       static int foo[ROW][N] ;
       return foo;
    }
    It looks like you should be using struct. Aren't you doing matrix addition?
    If you still want to stick with 2D array.
    void add( int a[] [N], int b[ ] [N], int result[] [ N] ) ;
    looks simpler.
    Read c-faq.
    Last edited by Bayint Naung; 07-30-2010 at 06:18 AM.

  12. #12
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Quote Originally Posted by Elysia View Post
    Yes, it can. However, its type is different:

    int myarr[10];
    int * pArr = myarr; // OK

    int myarr[5][10];
    int * pArr = myarr; // Not OK
    int (*pArr)[10] = myarr; // OK
    I see.....

    than can't i return a two dimensional array to main in my code?

    or can I by declaring it's prototype as follows

    int *[n] add(int [][n],int [][n]);

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could, but then again, why would you need to?
    int (*)[N] add(int [][N],int [][N]);
    And as pointed out, use upper-case for macros, and preferably more than one character.
    Something like SIZE would be a better choice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Thanks a lot!

    Just one more question!
    why did you recommended me to use upper case macro and more than one alphabet!

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To avoid name collision.
    Example:
    Code:
    #define n 10
    int main()
    {
        int n[10]; // Syntax error. Why?
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Help printing a multi array
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 05-05-2008, 01:35 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Passing a 2 dimension array to a function
    By Chuck in forum C++ Programming
    Replies: 8
    Last Post: 12-09-2002, 09:42 AM
  5. multi array to function
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-09-2001, 03:01 AM