Re: Feedback on C code?

You'll probably want to pass the board as a pointer along with the width and height, then do int value = board[x*height+width].  You can't pass arrays by value; if you want to do that, you'd do something like:

struct Board {
    int *data;
    size_t width, height;
};

int readBoard(struct Board *board, size_t x, size_t y) {
    return board->data[board->height*x+y];
}

then use memory allocation functions to allocate board.  ALternatively, put the array in the struct, which stops a thing called pointer decay from happening.

You want the width as well because that will let you check whether x is right--in practice you'd have a debug-only bounds check here with assert or something.

I had to look up the semantics, but basically arrays are converted to pointers when passed to functions, and int board[] is just a fancy way of saying pointer to int.  I had to look it up because it's not done in practice, since (in C at any rate) you can't make a function that works on any size of array.

C and C++ are different as well.  (most) C compiles as C++.  Very little C++ compiles as C.  C++ is something like literally 10 to 20 times as complicated as C.  I'd not try to get C++ resources teaching you C for that reason, though in this case you'd have the same problem in C++ as far as I know.  C++ people have a set of solutions to this that are sort of better, but explaining non-type template parameters is very much not worth it until after you've had 5 or 6 other explanations of C++ things.



-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Mitch via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector

Reply via email to