Subscript requires array or pointer type
Jul 14, 2015 at 9:00pm UTC
Hello everyone,
I could really use your help. I am confused about this error that I'm getting. I reread the chapter on pointers and arrays, but I'm still lost. error c2109 subscript requires array or pointer type. Here's my code:
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
int GameBoard::turn(int &playerTurn, int position)
{
char * pBoard = &board[ROW][COL];
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (playerTurn == 1)
{
if (position >= 1 && position <= 3)
{
*pBoard[0][position-1] == PLAYER_ONE;
playerTurn = 2;
}
else if (position >= 4 && position <= 6)
{
}
else if (position >= 7 && position <= 9)
{
}
}
}
}
return playerTurn;
}
Jul 14, 2015 at 9:27pm UTC
You do use subscript operators on lines 3 and 12. Which one has the error? (That information is part of the error message. Do not ignore it.)
At the bottom of
http://www.cplusplus.com/doc/tutorial/operators/
is an operator precedence table. The subscript has higher precedence than the dereference operator.
On line 12 you do write:
1 2 3 4 5 6 7
*foo[x][y]
// which is same as
*( (foo[x])[y] )
// However, the type of foo is char*
// Therefore, the type of foo[x] is char
Obviously, you cannot use subscript with a
char
.
You can reproduce the C2109 with:
1 2 3 4 5 6
int main() {
char bar = 'h' ;
int y = 7;
bar[y]; // C2109
return 0;
}
PS. What is the difference between
=
and
==
? (Line 12)
Last edited on Jul 14, 2015 at 9:27pm UTC
Jul 14, 2015 at 10:31pm UTC
The c2109 error is on line 12. I also just noticed the typo on line 12, and fixed it. I guess I'm a little confused on char arrays, I'll read up in my book and reply back later with more questions.
Topic archived. No new replies allowed.