Finding the location of a char in a 2D array

I create an multi dimensional array of dimensions 5 X 5.
I have the first character as, say P. all other's are X.
Now I print the array as a grid and I want it to move one character right if the user enters R, one up if the user enters U and so on.
But I don't know in which cell of the array is the P. So is there any straight forward way to do that? BTW, I was trying to avoid a loop here.
And this is the code that I tried to use:
1
2
*PlayerLoc = '.';
*(PlayerLoc+1) = PlayerPeg;

this is inside an switch statement. Also, PlayerLoc = &PlayerPeg, where PlayerPeg is 'P'.
I have tried this and instead of moving one right, it derectly goes to the very last block of the array (Map[4][4]) and replaces it with a
.

Any help?
Last edited on
Just store the x and y coordinates of the player.
By the way, http://www.cplusplus.com/forum/articles/28558/
Just store the x and y coordinates of the player.
By the way, http://www.cplusplus.com/forum/articles/28558/
Report

That will help me solve the problem.Thanks.

But why did the error happen in the code I gave?
I also tried it with *(PlayerPeg+1) = 'P', in place of line 2, the code returned a P in place of the "." in the last place

And I have actually read that article, this is just to practice with programming.
Post how you declare and define your array, playerloc and playerpeg. I can't really say anything now.
The Array:
1
2
3
4
5
6
7
8
9
10
11
Map[5][5]
//.....
for (int i = 0;i<5;i++)
{
	for (int j = 0;j<5;j++)
	{
		Map[i][j] = '.';
	}
}

Map[0][0] = PlayerPeg;

This is to make the array full of "."

PlayerPeg:
PlayerPeg = 'P'

PlayerLoc:

PlayerLoc = &PlayerPeg

PlayerPeg is a local variable which is not in any way bound to any element of Map. If you declared playerpeg as a reference (some other changes would be needed), it should work. Also, you could simply do playerloc = &map[0][0]. Either way should work.
Ah! Now I understand!
Thanks a lot!!
Topic archived. No new replies allowed.