How to return an array with 2 dimension from function?

Discussion in 'C Programming' started by MN, Aug 18, 2008.

  1. MN

    MN Guest

    I have 2 questions:
    1- Why isn't possible to declare a static char array [length][m+1]
    inside function ?
    the compiler gives this error: storage size of ‘array’ isn’t
    constant.

    2- Why isn't possible to output the whole array with 2 dimensions from
    function?
    my function looks like this:

    char* function(int m, int reverse, int length)
    {
    static char array [length][m+1]; /* +1 bit is for stroing the '\0'
    character*/
    ...

    /* Do some calculation and store each result in each array's
    element*/
    .....
    return (&array [ ] [m+1]); /* output all array's elements*/
    }
    Thanks for tacking a time to help me.
    MN, Aug 18, 2008
    #1
    1. Advertising

  2. MN

    James Kuyper Guest

    MN wrote:
    > I have 2 questions:
    > 1- Why isn't possible to declare a static char array [length][m+1]
    > inside function ?
    > the compiler gives this error: storage size of ‘array’ isn’t
    > constant.


    Because the memory for a static object must be set aside prior to the
    start of the program. Think like the compiler: prior to the start of the
    program, how would the compiler know how much memory to set aside for
    that array?

    > 2- Why isn't possible to output the whole array with 2 dimensions from
    > function?
    > my function looks like this:
    >
    > char* function(int m, int reverse, int length)
    > {
    > static char array [length][m+1]; /* +1 bit is for stroing the '\0'
    > character*/
    > ...
    >
    > /* Do some calculation and store each result in each array's
    > element*/
    > .....
    > return (&array [ ] [m+1]); /* output all array's elements*/
    > }


    I would recommend the following interface:

    void function(int m, int reverse, int length, char array[length][m+1))
    {
    /* Do some calculation and store each result in each array's element*/

    return;
    }

    That way, the memory for the array to be filled in is provided by the
    caller. A more error-prone approach would be to allocate the memory
    using malloc(), fill it in, and return a pointer to the allocated memory
    to the calling routine. I would be up to the caller to make sure that
    the memory was free()d when they were done with it.
    James Kuyper, Aug 18, 2008
    #2
    1. Advertising

  3. On 18 Aug, 13:07, MN <> wrote:
    >  I have 2 questions:
    > 1- Why isn't possible to declare a static char array [length][m+1]
    > inside function ?
    >    the compiler gives this error: storage size of ‘array’ isn’t
    > constant.
    >
    > 2- Why isn't possible to output the whole array with 2 dimensions from
    > function?
    > my function looks like this:
    >
    > char* function(int m, int reverse, int length)
    > {
    >     static char array [length][m+1]; /* +1 bit is for stroing the '\0'
    > character*/
    >     ...
    >
    >     /* Do some calculation and store each result in each array's
    > element*/
    >     .....
    >     return (&array [ ] [m+1]);   /* output all array's elements*/}
    >
    > Thanks for tacking a time to help me.


    Hi!

    You may have a static pointer initiated to NULL. then do a malloc the
    first time you run the function and let your static pointer point to
    the allocated area. (If the pointer is NULL, then it's the first
    time.) Don't forget to deallocate!

    You need to specify both rows and columns, introduce a zero and it
    shall work!
    return (&array [0] [m+1]);

    Ofcurse, if you use a static poitner and malloc as described, you just
    return the pointer.
    Andreas Lundgren, Aug 18, 2008
    #3
  4. Andreas Lundgren <> wrote:
    > On 18 Aug, 13:07, MN <> wrote:
    > >  I have 2 questions:
    > > 1- Why isn't possible to declare a static char array [length][m+1]
    > > inside function ?
    > >    the compiler gives this error: storage size of ‘array’ isn’t
    > > constant.
    > >
    > > 2- Why isn't possible to output the whole array with 2 dimensions from
    > > function?
    > > my function looks like this:
    > >
    > > char* function(int m, int reverse, int length)
    > > {
    > >     static char array [length][m+1]; /* +1 bit is for stroing the '\0'
    > > character*/
    > >     ...
    > >
    > >     /* Do some calculation and store each result in each array's
    > > element*/
    > >     .....
    > >     return (&array [ ] [m+1]);   /* output all array's elements*/}
    > >
    > > Thanks for tacking a time to help me.


    > Hi!


    > You may have a static pointer initiated to NULL. then do a malloc the
    > first time you run the function and let your static pointer point to
    > the allocated area. (If the pointer is NULL, then it's the first
    > time.) Don't forget to deallocate!


    > You need to specify both rows and columns, introduce a zero and it
    > shall work!
    > return (&array [0] [m+1]);


    I don't really understand what the OP wants to do, but returning
    '&array[0][m+1]' will result in a pointer to the second string in
    the array (which would better written as '&array[1][0]'). If the
    OP wants a pointer to the whole array (or, to be precise, to the
    first char in the array), he has to use

    return &array[0][0];

    or, simpler, just

    return array;
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Aug 18, 2008
    #4
  5. On Mon, 18 Aug 2008 12:07:09 GMT, James Kuyper
    <> wrote:

    > MN wrote:


    > > 2- Why isn't possible to output the whole array with 2 dimensions from
    > > function?
    > > my function looks like this:
    > >
    > > char* function(int m, int reverse, int length)
    > > {
    > > static char array [length][m+1]; /* +1 bit is for stroing the '\0'
    > > character*/


    > I would recommend the following interface:
    >
    > void function(int m, int reverse, int length, char array[length][m+1))


    Only in C99 (not yet widely implemented, see flamewar otherthread) or
    GNUC90 (i.e. an extension to C90 in GCC, which is not universal).

    > That way, the memory for the array to be filled in is provided by the
    > caller. A more error-prone approach would be to allocate the memory
    > using malloc(), fill it in, and return a pointer to the allocated memory
    > to the calling routine. I would be up to the caller to make sure that
    > the memory was free()d when they were done with it.


    I'm not sure that's more error-prone, but it is annoyingly asymmetric.

    In standard C90 you could also pass the bounds info and a pointer
    (here, char *) to space allocated (and later freed) by the caller,
    which you explicitly address like ptr[sub1*dim2+sub2] .
    Or for array of string as here like strcpy ( &ptr[sub1*dim2], src ).

    - formerly david.thompson1 || achar(64) || worldnet.att.net
    David Thompson, Sep 1, 2008
    #5
  6. MN

    James Kuyper Guest

    David Thompson wrote:
    > On Mon, 18 Aug 2008 12:07:09 GMT, James Kuyper
    > <> wrote:

    ....
    >> I would recommend the following interface:
    >>
    >> void function(int m, int reverse, int length, char array[length][m+1))

    >
    > Only in C99 (not yet widely implemented, see flamewar otherthread) or
    > GNUC90 (i.e. an extension to C90 in GCC, which is not universal).


    That doesn't change my recommendation. It just means that my
    recommendation implies that you should get and use a C99 compiler, or at
    least one that implements this particular C99 feature. If the compiler
    you would otherwise want to use doesn't provide this feature, pressure
    them to provide it, and preferably the rest of C99 as well.

    >> That way, the memory for the array to be filled in is provided by the
    >> caller. A more error-prone approach would be to allocate the memory
    >> using malloc(), fill it in, and return a pointer to the allocated memory
    >> to the calling routine. I would be up to the caller to make sure that
    >> the memory was free()d when they were done with it.

    >
    > I'm not sure that's more error-prone, but it is annoyingly asymmetric.


    It's error prone because users can forget to free() the memory.
    James Kuyper, Sep 1, 2008
    #6
    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. Shapper

    Array Dimension

    Shapper, Jun 7, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    455
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Jun 8, 2005
  2. Larry Lindsey
    Replies:
    5
    Views:
    524
    Mike Wahler
    Sep 27, 2003
  3. ssss
    Replies:
    2
    Views:
    367
    -berlin.de
    Nov 19, 2004
  4. snnn

    pass a 2-dimension array to function

    snnn, May 16, 2005, in forum: C Programming
    Replies:
    1
    Views:
    391
    S.Tobias
    May 18, 2005
  5. Luuk
    Replies:
    15
    Views:
    798
    Nobody
    Feb 11, 2010
Loading...

Share This Page