It is ok to return a struct variable from a function. How about an
array of any type from a function?
Because the array is allocated in the stack, it is no meaning to return
it, which ever type it is. This array is the memory of stack.
But for a struct, it is allocated
|
|
0
|
|
|
|
Reply
|
chming (3)
|
1/14/2005 7:14:19 AM |
|
chming wrote:
> It is ok to return a struct variable from a function. How about an
> array of any type from a function?
>
> Because the array is allocated in the stack, it is no meaning to return
> it, which ever type it is. This array is the memory of stack.
> But for a struct, it is allocated
>
Straight from the Standard:
6.7.5.3 Function declarators (including prototypes)
Constraints
[#1] A function declarator shall not specify a return type
that is a function type or an array type.
-- and --
6.9.1 Function definitions
[#3] The return type of a function shall be void or an
object type other than array type.
So in other words, no. You can, however, return blocks of memory
allocated using malloc() and friends. So if you wish to return the
contents of an array you may do so indirectly by copying it into a
buffer allocated with malloc() and returning its address.
--John
|
|
0
|
|
|
|
Reply
|
not3609 (1)
|
1/14/2005 8:01:59 AM
|
|
"John Valko" <not@available.info> wrote in message
news:cs7u6a$i5a$1@mailhub227.itcs.purdue.edu...
> chming wrote:
<snip>
> So if you wish to return the contents of an array you may do so indirectly
by copying it into a
> buffer allocated with malloc() and returning its address.
Of simply return a pointer to the array. Mind the scope of the array,
though.
|
|
0
|
|
|
|
Reply
|
dandelion (277)
|
1/14/2005 9:24:00 AM
|
|