So someone else made this to help me solve another question of mine. But I have 2 questions:
1. The grid points seem to be off by 1. Why? And how can I fix this?
2. Can someone explain this code to me in depth? Like, every detail and what "data" is? What is delete? so many questions.
Here's he code:
It doesn't really seem to be off, just, starts counting from 0. A simple "fix" would be to edit putPoint() and deduct 1 from the input value (and of course, tweak the constraints).
"data", as you can see, is declared as char** data, which is essentially, a pointer to a pointer of type char. Since we can use regular char* pointers to point to the first element of a dynamically allocated array, a char** pointer type can be used to point to the first element of the first dynamically allocated array. If this doesn't make sense to you, just treat it as a 2D array (which is how it is used in the code above). Accessing data, for example, will access element at row 1, column 2.
The delete keyword is used to free dynamically allocated memory (memory allocated using the 'new' keyword). If you allocated only one memory block, e.g. int* ptr = new int, then delete ptr would suffice. If you allocated more than that (i.e. an array), then use delete. E.g. int* ptr = new int; delete ptr;
I will agree with KrOW and Hatsy Rei , the indices will be off by one, but from what frame of reference? To a coder, they might suspect that the indices start at 0. Personally, if I were thinking in "grid mode", I would think that the X's were not on the axes (x=0, y=0), so I would (again, personally) make the function translate the grid coordinates to array coordinates. Just a thought I just had...happy coding!