By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,167 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,167 IT Pros & Developers. It's quick & easy.

returning an array of integers

P: n/a
Hi,

This might be a strange question but i would like to know how to
return an array from a function. Do you have to use pointers for this?

Thanks in advance,

Robert
Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Robert <ro****@nemesis-net.com> wrote in
news:ch**********@reader10.wxs.nl:
Hi,

This might be a strange question but i would like to know how to
return an array from a function. Do you have to use pointers for this?


We answer this often here. Hopefully I get it right.

typedef struct ArrayContainer
{
int array[1024];
} ArrayContainer;

ArrayContainer manipulateArray(ArrayContainer *pArr)
{
pArr->array[0] = 0xDEADBEEF;

return *pArr;
}

int main(void)
{
ArrayContainter arrOne = { 0 };
ArrayContainter arrTwo = manipulateArray(&arrOne);

// arrTwo.array[0] is now set to 0xDEADBEEF;

return 0;
}

--
- Mark ->
--
Nov 14 '05 #2

P: n/a
Mark A. Odell wrote:
Robert <ro****@nemesis-net.com> wrote in
news:ch**********@reader10.wxs.nl:

Hi,

This might be a strange question but i would like to know how to
return an array from a function. Do you have to use pointers for this?

We answer this often here. Hopefully I get it right.

typedef struct ArrayContainer
{
int array[1024];
} ArrayContainer;

ArrayContainer manipulateArray(ArrayContainer *pArr)
{
pArr->array[0] = 0xDEADBEEF;

return *pArr;
}

int main(void)
{
ArrayContainter arrOne = { 0 };
ArrayContainter arrTwo = manipulateArray(&arrOne);

// arrTwo.array[0] is now set to 0xDEADBEEF;

return 0;
}


So it isnt really possible to return an array but only a structure?
Nov 14 '05 #3

P: n/a
Robert wrote:
Mark A. Odell wrote:
Robert <ro****@nemesis-net.com> wrote in
news:ch**********@reader10.wxs.nl:
Hi,

This might be a strange question but i would like to know how to
return an array from a function. Do you have to use pointers for this?


We answer this often here. Hopefully I get it right.

typedef struct ArrayContainer
{
int array[1024];
} ArrayContainer;

ArrayContainer manipulateArray(ArrayContainer *pArr)
{
pArr->array[0] = 0xDEADBEEF;

return *pArr;
}

int main(void)
{
ArrayContainter arrOne = { 0 };
ArrayContainter arrTwo = manipulateArray(&arrOne);

// arrTwo.array[0] is now set to 0xDEADBEEF;

return 0;
}


So it isnt really possible to return an array but only a structure?


It isn't possible to return an array, just as it isn't
possible to pass an array as an argument or to assign one
array to another. People sometimes write that arrays aren't
"first-class" data objects in C; this is the sort of thing
they mean.

A function can, however, return a struct or a union,
and a struct or union can contain an array (a struct can
contain more than one, if desired). So even though a
function cannot return an array as such, it can return
a "wrapped" array that the caller can then "unwrap."

Often, though, this subterfuge is unsatisfactory. It
only works with arrays whose size is fixed at compile time;
if your array's size is going to be computed at run time
you're out of luck. Also, if the array is large it will
probably take a lot of time to slosh all that data back and
forth.

The usual way around these difficulties is to give up
on the idea of returning an actual array, and instead return
a pointer to the first element of the array. Pointers and
arrays are very nearly interchangeable for most purposes;
Section 6 of the comp.lang.c Frequently Asked Questions
(FAQ) list <http://www.eskimo.com/~scs/C-faq/top.html> has
a good exposition of the similarities and differences.
Questions 2.9 and 7.5 deserve your attention, too.

--
Er*********@sun.com

Nov 14 '05 #4

P: n/a
In <ch**********@reader10.wxs.nl> Robert <ro****@nemesis-net.com> writes:
So it isnt really possible to return an array but only a structure?


Right. There is no syntax in C for returning an array by value. The
obvious

return array;

returns the address of the first array element. This is why the array
must be encapsulated in a structure or union, which can be returned by
value.

This is one of the reasons arrays are not first class citizens in C.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5

P: n/a
Eric Sosman wrote:
Robert wrote:
Mark A. Odell wrote:
Robert <ro****@nemesis-net.com> wrote in
news:ch**********@reader10.wxs.nl:

Hi,

This might be a strange question but i would like to know how to
return an array from a function. Do you have to use pointers for this?


We answer this often here. Hopefully I get it right.

typedef struct ArrayContainer
{
int array[1024];
} ArrayContainer;

ArrayContainer manipulateArray(ArrayContainer *pArr)
{
pArr->array[0] = 0xDEADBEEF;

return *pArr;
}

int main(void)
{
ArrayContainter arrOne = { 0 };
ArrayContainter arrTwo = manipulateArray(&arrOne);

// arrTwo.array[0] is now set to 0xDEADBEEF;

return 0;
}


So it isnt really possible to return an array but only a structure?

It isn't possible to return an array, just as it isn't
possible to pass an array as an argument or to assign one
array to another. People sometimes write that arrays aren't
"first-class" data objects in C; this is the sort of thing
they mean.

A function can, however, return a struct or a union,
and a struct or union can contain an array (a struct can
contain more than one, if desired). So even though a
function cannot return an array as such, it can return
a "wrapped" array that the caller can then "unwrap."

Often, though, this subterfuge is unsatisfactory. It
only works with arrays whose size is fixed at compile time;
if your array's size is going to be computed at run time
you're out of luck. Also, if the array is large it will
probably take a lot of time to slosh all that data back and
forth.

The usual way around these difficulties is to give up
on the idea of returning an actual array, and instead return
a pointer to the first element of the array. Pointers and
arrays are very nearly interchangeable for most purposes;
Section 6 of the comp.lang.c Frequently Asked Questions
(FAQ) list <http://www.eskimo.com/~scs/C-faq/top.html> has
a good exposition of the similarities and differences.
Questions 2.9 and 7.5 deserve your attention, too.

Thank you both for the info :)
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.