|  | How to return an array with 2 dimension from function? |  | |
| | | MN |  |
| Posted: Mon Aug 18, 2008 11:07 am Post subject: How to return an array with 2 dimension from function? |  |
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. |
| |
| | | pete |  |
| Posted: Mon Aug 18, 2008 11:07 am Post subject: Re: How to return an array with 2 dimension from function? |  |
MN wrote:
| Quote: | 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.
|
Objects with static duration are initialized before main starts executing.
-- pete |
| |
| | | James Kuyper |  |
| Posted: Mon Aug 18, 2008 11:07 am Post subject: Re: How to return an array with 2 dimension from function? |  |
| |  | |
MN wrote:
| Quote: | 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?
| Quote: | 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. |
| |
| | | Jens Thoms Toerring |  |
| Posted: Mon Aug 18, 2008 11:07 am Post subject: Re: How to return an array with 2 dimension from function? |  |
| |  | |
Andreas Lundgren <d99alu@efd.lth.se> wrote:
| Quote: | On 18 Aug, 13:07, MN <mazouz.nezh...@gmail.com> 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 ___ jt@toerring.de \__________________________ LINK |
| |
| | | Andreas Lundgren |  |
| Posted: Mon Aug 18, 2008 12:18 pm Post subject: Re: How to return an array with 2 dimension from function? |  |
| |  | |
On 18 Aug, 13:07, MN <mazouz.nezh...@gmail.com> wrote:
| Quote: | 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. |
| |
| | | David Thompson |  |
| Posted: Mon Sep 01, 2008 4:53 am Post subject: Re: How to return an array with 2 dimension from function? |  |
| |  | |
On Mon, 18 Aug 2008 12:07:09 GMT, James Kuyper <jameskuyper@verizon.net> wrote:
| Quote: | 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).
| Quote: | 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 |
| |
| | | James Kuyper |  |
| Posted: Mon Sep 01, 2008 8:30 am Post subject: Re: How to return an array with 2 dimension from function? |  |
| |  | |
David Thompson wrote:
| Quote: | On Mon, 18 Aug 2008 12:07:09 GMT, James Kuyper jameskuyper@verizon.net> 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.
| Quote: | 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. |
| |
|
|