For a while I've had this struct:
struct coordinates{
struct coord coord_board[8][8];
};
And I've initialized it with
coordBoard = malloc(sizeof(struct coordinates));
for (col = 'a'; col <= 'h'; col++) {
for (row = '1'; row <= '8'; row++) {
initializeCoord(col, row);
}
}
void initializeCoord(char col, char row) {
coordBoard->coord_board[col][row].Iter = 0;
coordBoard->coord_board[col][row].occupant = NULL;
}
So this has actually worked for a while. I could even access them using chars too:
void printCoordBoard() {
char col, row;
printf("\n");
for (col = 'a'; col <= 'h'; col++) {
for (row = '1'; row <= '8'; row++) {
struct coord l = board->coordBoard->coord_board[col][row];
printf("%c ", l.occupant == NULL ? ' ' : l.occupant->type);
}
printf("\n");
}
}
And this has also worked. But now it doesn't - And I have no idea what I've done to make it fail. Though when I think about it, it makes sense. It's been a while since I've used C, but I remember that all arrays are accessed using an integer - right? I initialize them as array[8][8]. So when I access it using e.g. 'a', I'd actually access location 97 (decimal value of char a), right? And then of course I'd have memory corruption.
My question is then: Why did it work? And even more weird - if I change the array to a [7][7], it works again... I'm getting confused. Another question is - would there be a way in C to use chars to access the data, with having to initialize an array of the highest char (in this case 'h', which would crate a 104x104 array)?
I hope someone can enlighen me!
Have a great day.
'1'is not the same as1. 2. array indexes start at 0, not at 1