Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A function returning an array

103 views
Skip to first unread message

axc...@gmail.com

unread,
Oct 22, 2013, 9:22:38 PM10/22/13
to
Hi all,

If I have a 1D array and a function that returns this array, how do i do it?

Thanks!

Alf P. Steinbach

unread,
Oct 22, 2013, 11:29:50 PM10/22/13
to
On 23.10.2013 03:22, axc...@gmail.com wrote:
>
> If I have a 1D array and a function that returns this array, how do i do it?

For a general fixed size array, use std::array.

For a general variable length array, use std::vector.

For an array of numbers to be processed in the same way, use std::valarray.

For a string, use std::string or std::wstring, depending on the
character type.

You don't ask about 2D arrays, but the answer there is, for the general
case, to roll your own (e.g. based on std::vector) or use a third party
one such as the Boost matrix class. For the case of image, however, use
the Boost image class. Disclaimer: I haven't used that image class.


Cheers & hth.,

- Alf

axc...@gmail.com

unread,
Oct 23, 2013, 12:11:03 AM10/23/13
to
Thanks for your help firstly. What I tried is:

Function(int myArray[], int UpdateArray[])
{
for(int t=0; t < NumElement; t++)
{
UpdateArray[t] = myArray[t];
}

swap(UpdateArray[0], UpdateArray[1]);

return UpdateArray;
}

Something like this. Is the syntax correct? Can I just say return UpdateArray?

Ian Collins

unread,
Oct 23, 2013, 12:15:45 AM10/23/13
to
axc...@gmail.com wrote:

Please clean up the mess that awful google interface makes of your quotes!
>
> Thanks for your help firstly. What I tried is:
>
> Function(int myArray[], int UpdateArray[])
> {
> for(int t=0; t < NumElement; t++)
> {
> UpdateArray[t] = myArray[t];
> }
>
> swap(UpdateArray[0], UpdateArray[1]);
>
> return UpdateArray;
> }
>
> Something like this. Is the syntax correct? Can I just say return UpdateArray?

No, you can't return an array.

Did you read any of what Alf wrote?

Use the C++ language constructs he suggested!

--
Ian Collins

Rennie deGraaf

unread,
Oct 23, 2013, 12:42:24 AM10/23/13
to
First of all, it's *very* dangerous to get in the habbit of assuming
things about the lengths of raw C++ arrays. C++ does *not* check bounds
on raw arrays. If one (or both) of the two arrays passed into that
function happened to be smaller than NumElement, then you're looking at
undefined behaviour. Typically, you'll read (or write) past the end of
the array and get data corruption, crashes and/or points where a clever
hacker could inject arbitrary binary code into your program. So if you
must deal with raw arrays, ALWAYS pass the sizes around with them and
verify that every read or write is in bounds.

Whenever possible, try to use std::vector, std::array, and other
standard container classes that automatically check array bounds. (If
your instructor told you to use raw arrays for this assignment, please
tell him/her that comp.lang.c++ disapproves of your curriculum :)

Second of all, no, that syntax isn't correct. What error messages does
your compiler give you? They should point you in the right direction.

As for returning the array, look up "pass by value" and "pass by
reference".

Rennie

Tobias Müller

unread,
Oct 23, 2013, 8:13:45 AM10/23/13
to
Rennie deGraaf <deg...@domain.invalid> wrote:

[...]

> Whenever possible, try to use std::vector, std::array, and other
> standard container classes that automatically check array bounds.

std::vector and std::array do _not_ check bounds, at least not for
operator[]. But at least you always know the bounds.

> (If your instructor told you to use raw arrays for this assignment, please
> tell him/her that comp.lang.c++ disapproves of your curriculum :)

Learning about raw arrays is not necessarily a bad thing. At least to know
all the pitfalls and traps. As a programmer you can almost be sure that you
will encounter them in existing code, and if you have to modify that code
you better know what you are doing. Or at least you have a good reason to
replace it with a better alternative.

Tobi

Rennie deGraaf

unread,
Oct 23, 2013, 11:07:24 AM10/23/13
to
Please disregard my previous post, as it contains inaccuracies.

Paul ( The Troll )

unread,
Oct 23, 2013, 8:34:30 PM10/23/13
to
The function is missing the return type i.e:
returnType FunctionName( arg1, arg2 ) { function body }


Also you cannot pass arrays in and out of functions , you pass a pointer to the array thus the arguments would be of type int*. You can pass the array and it will automatically be converted to an int pointer, when calling the function you just use the arrays name ( no square brackets )i.e:

FunctionName( arrayName1, arrayName2);

HTH
Paul.


James Kanze

unread,
Oct 25, 2013, 10:14:26 AM10/25/13
to
On Wednesday, 23 October 2013 05:15:45 UTC+1, Ian Collins wrote:
> axc...@gmail.com wrote:
> Please clean up the mess that awful google interface makes of your quotes!
> > Thanks for your help firstly. What I tried is:

> > Function(int myArray[], int UpdateArray[])
> > {
> > for(int t=0; t < NumElement; t++)
> > {
> > UpdateArray[t] = myArray[t];
> > }
> > swap(UpdateArray[0], UpdateArray[1]);
> > return UpdateArray;
> > }

> > Something like this. Is the syntax correct? Can I just say
> > return UpdateArray?

> No, you can't return an array.

But UpdateArray isn't an array.

--
James

Jorgen Grahn

unread,
Oct 25, 2013, 4:48:31 PM10/25/13
to
On Wed, 2013-10-23, axc...@gmail.com wrote:
...
> Thanks for your help firstly. What I tried is:
>
> Function(int myArray[], int UpdateArray[])
> {
> for(int t=0; t < NumElement; t++)
> {
> UpdateArray[t] = myArray[t];
> }
>
> swap(UpdateArray[0], UpdateArray[1]);
>
> return UpdateArray;
> }

What you're doing here would, in C++, be done using iterators:

template<class It>
void Function(It src, size_t n, It dest)
{
std::copy(src, src+n, dest);
assert(n>1);
std::swap(*dest, *dest+1);
}

But that's rather far away from the concept of "returning an array".
You're not returning anything; you are copying. Have you been working
in Java a lot?

If you want to return an object, see earlier suggestions.

> Something like this. Is the syntax correct?

Don't you have a C++ compiler? It's better at catching syntax errors
than we are. It's part of its job.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Jorgen Grahn

unread,
Oct 25, 2013, 5:30:36 PM10/25/13
to
On Fri, 2013-10-25, Jorgen Grahn wrote:
...
> What you're doing here would, in C++, be done using iterators:
>
> template<class It>
> void Function(It src, size_t n, It dest)
> {
> std::copy(src, src+n, dest);
> assert(n>1);
> std::swap(*dest, *dest+1);
> }

I suppose that should be *(dest+1); it was just an untested sketch.

Ian Collins

unread,
Oct 25, 2013, 6:21:05 PM10/25/13
to
Good spot! On the ball as always.

--
Ian Collins
0 new messages