How to return an array with 2 dimension from function?

M

MN

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.
 
J

James Kuyper

MN said:
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.
 
A

Andreas Lundgren

 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.
 
J

Jens Thoms Toerring

Andreas Lundgren said:
 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.

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
 
D

David Thompson

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
 
J

James Kuyper

David said:
On Mon, 18 Aug 2008 12:07:09 GMT, James Kuyper
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.
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top