Pointers or References

I hope all is well programmers! I am currently trying to store some values in some variables within a local function and somehow use those variables outside of that local function. And I think you can use either pointers or references to accomplish this but I'm not exactly sure if that is possible or if it is, how. My question might be a little confusing but looking at the code will make things much clearer.

Any suggestions as to how to accomplish this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
        //without writing some global variable here. 
	cin >> player_one_input;
	for (int j = 0; j < board_size; j++)
	{
		for (int k = 0; k < board_size; k++)
		{
			if (p_p_omok_board[j][k] == player_one_input)
			{
				//Some code here to store j and k
                                //Perhaps a pointer here or a reference?
			}
		}
	}
        //somehow be able to use j and k over here.  
Last edited on
Sorry, but your code does not help that much as you talk about functions and globals but just show code which looks like it comes from a single function (and that you're somehow wanting to use variables defined within the scope of a for loop after it?)

Do you mean this sort of thing?

(I swapped you variable names from j, k to row, col as they make things clearer to me when dealing with a 2d 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
50
51
52
#include <iostream>
using namespace std;

const int board_size = 4;

int p_p_omok_board[board_size][board_size] =
{ { 1,  2,  3,  4},
  { 5,  6,  7,  8},
  { 9, 10, 11, 12},
  {13, 14, 15, 16}  };

void test(int& row_found, int& col_found)
{
    row_found = -1; // set variables to -1 for not found
    col_found = -1; // (following usual convention)

    int player_one_input = 0;
    cout << "input? ";
    cin >> player_one_input;
    for (int row = 0; row < board_size; row++)
    {
        for (int col = 0; col < board_size; col++)
        {
            if (p_p_omok_board[row][col] == player_one_input)
            {
                row_found = row;
                col_found = col;
                return; // no need to continue looping
            }
        }
    }
}

int main()
{
    int row_found = 0;
    int col_found = 0;

    test(row_found, col_found);

    if((-1 != row_found) && (-1 != col_found))
    {
        cout << "found at (" << row_found  << ", "
             << col_found << ")\n";
    }
    else
    {
        cout << "not found\n";
    }

    return 0;
}


If so, you could use a function of the form

void test(int* p_row_found, int* p_col_found)

but then you'd have to dereference and guard the pointers. In this case there's no point. But it can be useful if you want to indicate whether you want a bit of information returned or not.

You could also return a struct or class type that contains the coords.

Andy
Last edited on
@Andywestken

Thank you so much for your response.

Yup. That's EXACTLY what I was looking for. Thank you!

Seriously, Andy, I just want to say thank you. You help me sooooo often. And it really makes learning so easy and so much better. Thank you again.

Best regards,
Jae Kim
Last edited on
And it really makes learning so easy and so much better. Thank you again.

But you should try and find things out yourself. As well as learning about C++ you should be learning how to solve things ground up.
Last edited on
@andywestken

Okay. I really do try to figure it out. I usually spend at least 30 min - 1 hour struggling with a problem until I post on the forum.

How come when I initialize row_found = -1 and col_found = -1 both in main and in my function get_value, I get a segment dump error message? And when I set them to row_found = 0 and col_found = 0, it works fine?

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std; 

void set_board (string **p_p_omok_board, int board_size)
{
	int seq_number = 0; 
	for (int i = 0; i < board_size; i++)
	{
		for (int j = 0; j < board_size; j++)
		{
			ostringstream convert; 
			convert << seq_number;  
			p_p_omok_board[i][j] = convert.str();
			seq_number++; 
		}
	}
}

void set_check_win (int **p_p_check_win, int board_size)
{
	for (int i = 0; i < board_size; i++)
	{
		for (int j = 0; j < board_size; j++)
		{  
			p_p_check_win[i][j] = 2;
		}
	}
}

void find_value (int &row_found, int &col_found, string player_input, string **p_p_omok_board, int board_size)
{
	row_found = -1; 
	col_found = -1; 
	for (int row = 0; row < board_size; row++)
	{
		for (int col = 0; col < board_size; col++)
		{
			if (p_p_omok_board[row][col] == player_input)
			{
				row_found = row; 
				col_found = col; 
				return; 
			}
		}
	}
}

void print_board (string **p_p_omok_board, int board_size)
{
	if (board_size <= 10)
	{
		cout << endl;
		for (int i = 0; i < board_size; i++)
		{
			for (int j = 0; j < board_size; j++)
			{
				cout << "|";
				cout.width(2);
				cout << left << p_p_omok_board[i][j]; 
			}
			cout << "|" << endl;
			cout << "-"; 
			for (int j = 0; j < board_size; j++)
			{
			cout << "---";
			}
			cout << endl; 
		}
	}
	if (board_size > 10)
	{
		cout << endl;
		for (int i = 0; i < board_size; i++)
		{
			for (int j = 0; j < board_size; j++)
			{
				cout << "|";
				cout.width(3);
				cout << left << p_p_omok_board[i][j]; 
			}
			cout << "|" << endl;
			cout << "-"; 
			for (int j = 0; j < board_size; j++)
			{
			cout << "----";
			}
			cout << endl; 
		}
	}
}

void free_board (string **p_p_omok_board, int board_size)
{
	for (int i = 0; i < board_size; i++)
	{
		delete [] p_p_omok_board[i]; 
	}
	delete [] p_p_omok_board; 
}

int main ()
{	
	int board_size = 0; 
	cout << "Enter board size: "; 
	cin >> board_size; 
	int max_num_of_moves = board_size * board_size; 
	int player_one_move = 0; 
	string player_one_input;
	int player_two_move = 1;  
	string player_two_input; 

	string **p_p_omok_board; 
	p_p_omok_board = new string*[board_size]; 
	for (int i = 0; i < board_size; i++)
	{
		p_p_omok_board[i] = new string[board_size]; 
	}

	int **p_p_check_win; 
	p_p_check_win = new int*[board_size]; 
	for (int i = 0; i < board_size; i++)
	{
		p_p_check_win[i] = new int[board_size]; 
	}

	set_check_win (p_p_check_win, board_size);  //sets all the elements in the array to 2. 
	set_board (p_p_omok_board, board_size); 
	print_board (p_p_omok_board, board_size); 
 
	for (int i = 0; i < max_num_of_moves; i++)
	{
		if (player_one_move == i)
		{
			while (cin >> player_one_input)
			{
				int row_found = -1; 
				int col_found = -1; 
				find_value (row_found, col_found, player_one_input, p_p_omok_board, board_size); 
				if (p_p_check_win[row_found][col_found] == 0 || p_p_check_win[row_found][col_found] == 1)
				{
					cout << "Invalid move!" << endl;
				}
				else
				{
					break;
				}
			}

				for (int row = 0; row < board_size; row++)
				{
					for (int col = 0; col < board_size; col++)
					{
						if (p_p_omok_board[row][col] == player_one_input)
						{
							p_p_omok_board[row][col] = "o";
							p_p_check_win[row][col] = 0; 
						}
					}
				}
			
			player_one_move = player_one_move + 2; 
		}
		else if (player_two_move == i)
		{
			cin >> player_two_input;
			for (int row = 0; row < board_size; row++)
			{
				for (int col = 0; col < board_size; col++)
				{
					if (p_p_omok_board[row][col] == player_two_input)
					{
						p_p_omok_board[row][col] = "x";
						p_p_check_win[row][col] = 1; 
					}
				}
			}
			player_two_move = player_two_move + 2; 
		}
		print_board (p_p_omok_board, board_size); 
	}

	free_board (p_p_omok_board, board_size); 
	return 0; 
}


Last edited on
How come when I initialize row_found = -1 and col_found = -1 both in main and in my function get_value, I get a segment dump error message? And when I set them to row_found = 0 and col_found = 0, it works fine?

Your code works fine for if I enter a valid number (one display by the board) but crashes when I enter some other, invalid number.

When find_value() gives you row_found = -1, col_found -1 you should not try to use these values. They're saying no value was found.

(If it helps with logic, you could modify find_value() so it returns true or false to indicate whether or not it found a match.)

Andy
Last edited on
@andywestken

Thank you so much. I understand. But in most cases, I should try to use -1 to indicate not found? That's what most programmers consider as "standard"?
I should try to use -1 to indicate not found?

Well, it is one common convention. Used when returning a 0 based index via an int. All valid results are 0 or greater. So you could test for invalid resuls using value < 0.

std::string uses an unsigned type for indexing, so the value std::string::find() returns when it fails is the maximum value for the type (as denoted by the const std::string::npos.) But one way of getting the maxmium value of an unsiged type is by casting -1 (a signed value) to the unsigned type, e.g.
const unsigned int max_unsigned_int = static_cast<unsigned int>(-1);

Andy
Topic archived. No new replies allowed.