| physj314 (6) | |
|
I was wondering if there was a way to transfer the values of an array created in a function to an array in a different area of the program. I know this probably unclear. i.e. #include <iostream> using namespace std; int array(int x, int y) { int num[2]; num[0] = x; num[1] = y; return num[]; /*I was wondering if it were possible to transfer this array down to the int main() when the function was called. */ } int main() { int a=3; int b=2; int nums[2]; /*how would one transfer the values of the num[] array from the function array() to the array nums[]. */ } | |
|
Last edited on
|
|
| Peter87 (3917) | |||
You can pass a pointer to the first element in the array and have the function set the elements in the array.
| |||
|
|
|||
| cire (2355) | |||
Note that there is an entity in the std namespace called "array", so using namespace std; isn't advised here, if you're going to use "array" as a name.
| |||
|
|
|||
| physj314 (6) | |
|
Ok thanks; thats useful. Also, is it possible to test if two arrays are equal to each other. Like: int num [] = {1,2,3}; int order [] = {1,3,2}; is it possible to easily test whether these two arrays are equivalent. | |
|
|
|
| fun2code (1227) | |||
I suppose 1st compare the sizes. If the sizes match then proceed to an element by element comparison.
| |||
|
Last edited on
|
|||
| Cubbi (1927) | |||
If you could use vectors or at least the C++ arrays cire mentioned, it would be simpler:
demo: http://ideone.com/7LdZAA | |||
|
Last edited on
|
|||