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[]. */
}
Note that there is an entity in the std namespace called "array", so usingnamespace std; isn't advised here, if you're going to use "array" as a name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
void set_array( int* a, int x, int y )
{
a[0] = x ;
a[1] = y ;
}
int main()
{
int nums[2] ;
int a=3, b=2 ;
set_array(nums, a, b) ;
std::cout << nums[0] << ", " << nums[1] << '\n' ;
}
I suppose 1st compare the sizes. If the sizes match then proceed to an element by element comparison.
1 2 3 4 5 6 7
bool are_equal( int A[], unsigned szA, int B[], unsigned szB )
{
if( szA != szB ) returnfalse;// A, B can't be equal
for(unsigned i = 0; i < szA; ++i )
if( A[i] != B[i] ) returnfalse;// once is all it takes
returntrue;
}