returning an array from a function

closed account (96AX92yv)
hello,
I am trying to neaten my code by putting them in different cpp files but in one function an array is changed:

int function(int array2[100][9])
{
for (int i =0; i < 100; i++)
{
for (int j = 0; j < 9; j++)
{
array2[i][j] = rnd(10);
}
}
return array2;
}

this is called by:

array = function (array);

array is declared as
int array[100][9];

does anyone know how to do this?
you do not need to return the array. Everything is done with array. Why do you want to return it?
closed account (96AX92yv)
they are in different cpp files. it has to be returned for so it can be used to change properties of objects
Last edited on
it doesn't matter if there are other cpp files.

This
array2[i][j] = rnd(10);
is what changes the array. You get the changes outside of the function whether you return the array or not

in other words: what you pass to your function is not a copy it's a pointer to the first element of the array.
Last edited on
closed account (96AX92yv)
thanks
im guessing this is the same for non-array vatiables aswel
Last edited on
Whe you have a function declared like this

int function(int array2[100][9]);

when you call it, you are passing the array by pointer, not by value. So, as coder777 has already said, any changes made to array2 in function are actually being made to the original array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int rows = 5;
const int cols = 3;

void fill_in(int array2[rows][cols]);
void print_out(int array3[rows][cols]);

int main()
{
	srand(time(0));

	int array1[rows][cols];

	fill_in(array1);
	print_out(array1);

	return 0;
}

void fill_in(int array2[rows][cols])
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			// array2 is a pointer to array1, so this
			// is changing array1
			array2[i][j] = rand() % 10; // or whatever
		}
	}
}

void print_out(int array3[rows][cols])
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			// array3 is also a pointer to array1, so this
			// is printing out array1 (not a copy)
			cout << " " << array3[i][j];
		}
		cout << endl;
	}
}


Output:

 3 5 0
 9 0 6
 8 9 6
 9 8 4
 9 6 2


Also, arrays are not assignable. So you can't even do this:

1
2
3
4
5
6
7
int array1[100][9];

// fill in

int array2[100][9];

array1 = array2; // error 


Instead, you have to copy the elements from one to other. Either element by element or byte-wise, using memcpy.

Andy
Last edited on
closed account (96AX92yv)
thanks
im guessing this is the same for non-array vatiables aswel
no, in order to change a variable that is passed as a parameter you need to pass it as a reference (&) or pointer (*):

int &x or int *x

for simple types it is better to pass it by value and return the (modified) value.
closed account (96AX92yv)
thanks Again
PS

Please check out:

How to use code tags
http://www.cplusplus.com/articles/jEywvCM9/

You can even go back and add tags to your earlier posts...

Andy
Topic archived. No new replies allowed.