Question

A rather quick question concerning pointers in c++

My problem is,let's say I have a function isWon(char * sign, int i, int j). I call this method by giving

  • the address of an element in a 2D array
  • it's coordinates in a locally declared array

Is there any way of e.g. knowing the elements neighbors and getting to them?

Thanks for the help :)

Was it helpful?

Solution

If the array is a true array 2D array and not an array of pointers or something like that, then you can add/subtract to/from sign to get other elements' addresses.

For example, memory-wise the previous element in the array is at sign - 1. If you think of your 2D array as a grid, sign - 1 might not be the element in the previous "column".


You have to be careful how much you step in your array and ask yourself why you resort to such low-level dangerous mechanisms that feel out of place in C++.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top