473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

returning an array of integers

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
5 1892
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
19
by: Steven T. Hatton | last post by:
I believe it is technically possible to return a pointer to the first element of an array. I can persuade the returned pointer to act like an array, with some qualifications. I'm specifically...
4
by: William | last post by:
I would appreciate your help on the following programming questions: 1. Given an array of length N containing integers between 1 and N, determine if it contains any duplicates. HINT: The...
5
by: J Lake | last post by:
I am working on a simple orderform script to keep a running total, however I am encountering some errors. function CalculateTotal() { var order_total = 0 // Run through all the form fields...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
8
by: engaref | last post by:
Hello Every body, I am new with C programming.I have received the Problems from my advisor on Array but I did not find any Proper answer yet. If Possible,please make a solution for the Problems....
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
5
by: Cromulent | last post by:
Okay I'm having a few issues with this and I can't seem to get it sorted out (most likely due to my inexperience with Python). Here is my Python code: def fileInput(): data = s =...
11
by: Peter | last post by:
I have written this small app to explain an issue I'm having with a larger program. In the following code I'm taking 10 ints from the keyboard. In the call to average() these 10 ints are then...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.