473,609 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1907
Robert <ro****@nemes is-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****@nemes is-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****@nemes is-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**********@r eader10.wxs.nl> Robert <ro****@nemes is-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****@nemes is-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
10111
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 code... TCHAR myArray; DoStuff(myArray);
19
1929
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 interested in multidimensional arrays. It is often said that arrays and pointers are virtually identical. My observations are that my (gcc) compiler knows the difference between T*, T a1, and a2. What I'm currently trying to do is to return a...
4
4255
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 solution is: http://domino.research.ibm.com/Comm/wwwr_ponder.nsf/solutions/January2004.html but I don't understand what they meant by "Let f(x) be the location of the
5
2243
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 for (var i=0; i < document.orderform.chkEvent.length - 1; ++i) { // Is the checkbox checked?
41
3792
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 = rand(); y = rand();
8
1686
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. Thanks The Problems are as follows: 1-Declare an array of length of 10 and read integers into the elements
7
6422
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 now thown out of the array released properly by the CLI?
5
1386
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 = raw_input("Please enter the filename to process (enter full path
11
2060
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 added and Divided by the number of ints to return the average, a float. Can someone see why I get for example: if the total is 227 then divided by 10 I get a return of 22.00000 instead of the correct answer 22.7.
0
8139
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8579
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8555
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8408
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7024
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5524
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4098
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.