Pass by reference issues.

Hello there I am currently beginning to learn C++ and I'm trying to make a CodeBreaker sort of game (Mastermind). However I have run into a problem in the calcResponse function.

I primarily program in Java so I am used to things just passing by value however this is not the case in C++. The calcResponse function has two int array inputs and will print B or W depending on how these two arrays compare. In order to do this the arrays need to be modified. This works fine for the 1234 code, however now the answer code has been modified it no longer works with the other codes (as it is now 0000).

What I need it to do is pass by value rather than by reference, which I am very confused about as I am not sure how to do this (especially with arrays!).

So I ask could someone explain how this can be done?
Is it due to the fact that arrays are a sort of reference "thing"? I could do with an explanation on that too.
Thank you very much, I appreciate any response :)

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 //CodeBreakerCPP
//Attempt to crack the code!
#include <iostream>
using namespace std;

//Global Constants
const char black = 'B';
const char white = 'W';

//Function Prototypes
void instructions();
void displayBoard(int[10][4],int[4]);
void calcResponse(int[4],int[4]);

//Main Function
int main(){
	int move;
	//instructions();
	//TODO: SetCodeOrCrack? [Default: Crack]
	int ans [4] = {1,2,3,4};
	int codes [10][4];
	for(int i=0;i<10;i++){
		for(int j=0;j<4;j++){
			codes[i][j]=i+j;
		}
	}
	displayBoard(codes,ans);
	return 0;
}

//Functions
void instructions()
{
    cout << "Welcome to the ultimate man-machine showdown: Tic-Tac-Toe.\n";
    cout << "--where human brain is pit against silicon processor\n\n";
    cout << "Make your move known by entering a number, 0 - 8.  The number\n";
    cout << "corresponds to the desired board position, as illustrated:\n\n";   
    cout << "       0 | 1 | 2\n";
    cout << "       ---------\n";
    cout << "       3 | 4 | 5\n";
    cout << "       ---------\n";
    cout << "       6 | 7 | 8\n\n";
    cout << "Prepare yourself, human.  The battle is about to begin.\n\n";
}

void displayBoard(int codes[10][4],int ans[4])
{
	for(int p=0;p<9;p++)
	{
    cout << "--------------------------\n";
    cout << "|.0"<<p+1<<"| "<<codes[p][0]<<" | "<<codes[p][1]<<" | "<<codes[p][2]<<" | "<<codes[p][3]<<" |";
    calcResponse(codes[p],ans);
    cout<<"\n";
	}
    cout << "--------------------------\n";
    cout << "|.10| "<<codes[9][0]<<" | "<<codes[9][1]<<" | "<<codes[9][2]<<" | "<<codes[9][3]<<" |\n";
	cout << "--------------------------";
}

void calcResponse(int code[4],int ans[4]){
	if(code[0]==0){
		return;
	}
	int B=0,W=0;
	for(int i=0;i<4;i++){
		if(code[i]==ans[i]){
			B++;
			code[i]=0;
			ans[i]=0;
		}
	}
	for (int i=0;i<4;i++){
		if(ans[i]==0){
			continue;
		}
		for(int j=0;j<4;j++){
			if(ans[i]==code[j]){
				W++;
				ans[i]=0;
				code[j]=0;
				break;
			}
		}
	}
	for(int i=0;i<B;i++){
		cout<<black;
	}
	for(int i=0;i<W;i++){
		cout<<white;
	}
}
I think you have a slight misunderstanding:
Pass by value: the value is copied, but the changes are NOT reflected:
1
2
3
4
5
6
7
8
9
void f(int x) {
   x = 5;
}

int main() {
   int a = 2;
   f(a); //passed by value
   //a is still 2
}


Pass by reference: you are modifying the actual variable:
1
2
3
4
5
6
7
8
9
10
void f(int& x) {
   //passed by value so
   x = 5;
}

int main() {
   int a = 2;
   f(a); //passed by reference
   //a is now 5
}


Raw arrays cannot be passed by value as you have seen, as they will decay to a pointer. If you want to not modify it, you will have to make a copy yourself.

Or, you can use std::array (if your compiler supports C++11) or std::vector. The exhibit the "normal" value/reference semantics (the same as int, etc).
Topic archived. No new replies allowed.