Arrays can be modifed when passed as arguments??

Hi, this is my first post here and I'm a beginner, so please be gentle!

I was writing some code that involved passing an array to a class method and I noticed that the method was able to modify the array, even if it was of the type void. This doesn't happen when other variable types such as int are passed. I think this is because a pointer to the first element is passed rather than the array itself, so it's like passing a variable by reference. In my code I actually don't want the array used as the parameter to be modified outside of the method, is there any way to achieve this other than making a copy of the array before passing?

How would I adjust the following code to stop the array getting modified by mod_array()? Is it even possible?

Thanks

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
50
  #include <iostream>
using namespace std;

class A {
	public:
	int static integer;
	int static array[];
};

int A::integer = 9;

int A::array[] = { 6, 7, 8 };

class B {
	public:
	void mod_array(int arr[]);
	void mod_int(int x);
};

void B::mod_array(int arr[]) {
	int temp;
	temp = arr[2];
	arr[2] = arr[0];
	arr[0] = temp;
}

void B::mod_int(int x) {
	x = 7;
}

int main() {
	
	A a;
	B b;
	
	for(int x = 0; x < 3; x++) { 
		cout << a.array[x] << " "; 
	}
	cout << "\n" << a.integer << "\n";
	
	b.mod_array(a.array);
	b.mod_int(a.integer);
	
	for(int x = 0; x < 3; x++) { 
		cout << a.array[x] << " "; 
	}
	cout << "\n" << a.integer << "\n";
	
	return 0;
}
Surely, since array is passed as a pointer, not copied.

(Though of course you could not make it point to other array - pointer itself is preserved)
Last edited on
Use the const keyword before the parameter name that you do not want to be modified.

void mod_array(const int arr[]);

1
2
3
4
5
6
void B::mod_array(const int arr[]) {
    int temp;
    temp = arr[2];
    arr[2] = arr[0];
    arr[0] = temp;
}


Note: do this and the above code will now give a compile error where the array is modified.
Thanks Chervil but what I want is for the array to be modifiable within the method mod_array() but for the original array to be unaffected. Using const means I can't modify the array within mod_array().

This example is trivial so maybe I should say how I encountered this problem. As an exercise I am making a poker game. The players 7 cards (2 hole and 5 community) are stored in an array called rank_array, which holds values from 2 to 14 (where 12 is a queen, 14 is an Ace). This array is passed to a method of another class that sorts it numerically then works out if the player has a pair or a straight etc. But I don't want the original array to be sorted because I use it later in another class to display the cards.
what I want is for the array to be modifiable within the method mod_array() but for the original array to be unaffected.

Because the parameter passed is simply a pointer to the array, there is only one array here. If you modify its contents, it will stay modified.

A solution would be to create a local array within function mod_array() and use a loop to copy the contents from the input array to the local array.
I think that is probably the solution then, thanks for your time
If you do that, then I would still use the const keyword, it both documents your intentions, as well as getting the compiler to check that your code is complying with what you told it you wanted to do.
Topic archived. No new replies allowed.