0

Hey i've been bothering with contiguous and linear allocated memory spaces out of good habit purposes.

I've been wondering if there is in somehow a capability to simply compare 2 Contiguously allocated 2D Array's.

Both are allocated like these, both the board and the game piece.

map->board = malloc(sizeof(char[map->x][map->y + 1]));

a board looks like this

000 ..............................
001 ..............................
002 ..X...........................
003 ..............................
004 ..............................
005 ..............................
006 ..............................
007 ..............................
008 ..............................
009 ..............................
010 ..............................
011 ...........................O..
012 ..............................
013 ..............................

The pieces look like these.

Piece 4 7 Piece 4 5: Piece 3 6:
...*... .**.. .****.
...*... .***. **....
...*... ..*.. *.....
..***.. .....

The purpose is for a piece to overlap '1' already placed token, and not exceed the dimensions.

However i'm unable to properly check if a piece exceeds the boards dimensions because it is a single dimensional array allocated with the size of a 2D Array, nor am i able to compare multiple rows.

Would i be better off just using Pointer-to-Pointer arrays?

VADE
  • 5
  • 3
  • 1
    You can compare arrays with `memcmp` but if you are comparing `struct`s (or arrays of) then unused space in the `struct` padding might cause an apparent difference which isn't effectively so. It's unclear what you are asking though: the question seems to go on to ask if you can compare the value of every element against a limit, with a single instruction. – Weather Vane Jun 23 '20 at 10:14
  • @WeatherVane Thanks for your reply i've updated the post to be more informative. – VADE Jun 23 '20 at 10:23
  • You can do the 1D to 2D conversions explicitly, or you could use a pointer to a variably defined array, e.g. `char (*board)[map->y + 1] = (char (*)[map->y + 1])map->board;` and access the elements as `board[i][j]`. – Ian Abbott Jun 23 '20 at 10:36

0 Answers0