Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to return an array with 2 dimension from function?

Reply
Thread Tools

How to return an array with 2 dimension from function?

 
 
MN
Guest
Posts: n/a
 
      08-18-2008
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 isnt
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.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      08-18-2008
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 isnt
> 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.
 
Reply With Quote
 
 
 
 
Andreas Lundgren
Guest
Posts: n/a
 
      08-18-2008
On 18 Aug, 13:07, MN <(E-Mail Removed)> 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 isnt
> 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.
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      08-18-2008
Andreas Lundgren <(E-Mail Removed)> wrote:
> On 18 Aug, 13:07, MN <(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      09-01-2008
On Mon, 18 Aug 2008 12:07:09 GMT, James Kuyper
<(E-Mail Removed)> 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
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      09-01-2008
David Thompson wrote:
> On Mon, 18 Aug 2008 12:07:09 GMT, James Kuyper
> <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
a question about 1-dimension and 2-dimension array Luuk C Programming 15 02-11-2010 02:45 AM
Array Dimension Shapper ASP .Net 1 06-08-2005 01:31 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
newbie question : picture dimension vs print dimension Rene Wong Digital Photography 9 09-30-2003 01:46 AM



Advertisments