A function returning an array

A

axcytz

Hi all,

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

Thanks!
 
A

Alf P. Steinbach

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
 
A

axcytz

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

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?
 
I

Ian Collins

(e-mail address removed) 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!
 
R

Rennie deGraaf

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

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?


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
 
T

Tobias Müller

[...]
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
 
R

Rennie deGraaf

On 23.10.2013 03:22, (e-mail address removed) 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

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?


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

Please disregard my previous post, as it contains inaccuracies.
 
P

Paul ( The Troll )

On 23.10.2013 03:22, (e-mail address removed) wrote:





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



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 UpdateArr

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 tothe array thus the arguments would be of type int*. You can pass the arrayand it will automatically be converted to an int pointer, when calling thefunction you just use the arrays name ( no square brackets )i.e:

FunctionName( arrayName1, arrayName2);

HTH
Paul.
 
J

James Kanze

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.
 
J

Jorgen Grahn

.
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
 
J

Jorgen Grahn

.
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.

/Jorgen
 
I

Ian Collins

James said:
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.

Good spot! On the ball as always.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top