c - "error: subscripted value is neither array nor pointer nor vector", but why? -
i writing tic-tac-toe program , writing function player's turn. passing in tic-tac-toe board (a 3x3 array) in form of pointer, b
. problem on last line error in title.
subscripted value neither array nor pointer nor vector: b[playercoordsx][playercoordsy] = "x";
just testing i've tried multiple different =
values. both characters , numerical values not fix issue.
here abbreviated code (what hope) relevant bits:
void playerturn(int *b); ... int main(void) { int board[2][2]; int (*b)[2][2]; b = &board; ... void playerturn(int *b); ... return 0; } void playerturn(int *b) { int playercoordsx, playercoordsy; while ((playercoordsx != 1 || playercoordsx != 2 || playercoordsx != 3) && (playercoordsy != 1 || playercoordsy != 2 || playercoordsy != 3)) { printf("enter x coordinate use:"); scanf("%i", &playercoordsx); playercoordsx = playercoordsx - 1; printf("enter y coordinate use:"); scanf("%i", &playercoordsy); playercoordsx = playercoordsy - 1; } b[playercoordsx][playercoordsy] = "x"; }
since b
pointer int,
b[playercoordsx]
is integer, cannot subscript b[playercoordsx]
. need pointer pointer int:
int **b;
then can double indirection. or, if have flat array, calculate index instead of using double indices:
b[playercoordsy * numcols + playercoordsx] = "x";
if define board do:
int board[3][3];
then can change function signature to:
void playerturn(int b[][3])
Comments
Post a Comment