0

I am creating a 2D-Array in C++ and need to pass this array as a parameter in a function. In my function, I need to access an element from the array in order to save it as a value, i.e.:

int lowestPoint(int **arr, int x, int y, int n) {

    minVal = *(*(arr+x)+y); // here is where I'm getting the exception
    return minVal;

}

I've tried setting minVal to arr[X][Y] and have tried to pass the array in as other variations instead of just **arr but nothing seems to be working.

The array is initialized in my main function as int arr[x][y] and I pass it into another function by casting it as otherFunc(reinterpret_cast<int **>((*arr)[n]), n), and then from that function, send it to lowestPoint by calling int val = lowestPoint(arr,i,j,n). I think these calls could be problematic but I'm uncertain how to fix it - I really have no experience with 2D arrays in C++ and it's soo much simpler in Java. I keep getting an EXC_BAD_ACCESS error for the array, so if anyone has any idea how to fix that, I'd really appreciate it. Thanks!

EDIT: "n" is the size of the array; for example if it's a 3x3 array, n = 3. I just initialized the array as int arr[n][n] and then stored elements. I know the actual array itself represents the correct value, it just can't access it once I send it to another function.

jess
  • 1
  • 2
  • We need to see how you made the 2D array, but my general recommendation in C++ is not make dynamic 2D array. Make a 1D array (preferably a `std::vector`) and [wrap it in a structure so that it looks 2D](https://stackoverflow.com/a/2076668/4581301). – user4581301 Oct 15 '19 at 03:49
  • 2
    Possible duplicate of [conversion of 2D array to pointer-to-pointer](https://stackoverflow.com/questions/8203700/conversion-of-2d-array-to-pointer-to-pointer) – Matriac Oct 15 '19 at 03:50
  • What is this `int n` for ? – ignacio Oct 15 '19 at 03:53
  • Never use reinterpret_cast unless you know that the type of the thing you are casting is the same as the destination type - the only really good use for reinterpret_cast is to cast a structure to be the type of the first element in the structure because that address really does have two types because the standard guarantees that the first item is at the same address as the structure. In general, casting of any kind is a bad thing - and there are very few situations that really require it. So casting `(*arr)[n]` to be a `int**` is wrong because `arr[0][n]` is an `int` - not an `int**` – Jerry Jeremiah Oct 15 '19 at 04:41

1 Answers1

1

When you pass the array to the first function using reinterpret_cast((*arr)[n]), instead of passing the pointer to the actual array, you are passing the value in location [0][n] (by using (*arr)[n]) and casting it to **arr. So in essence you get a new array that points to a random location in memory that is equal to the content of that array slot.

I am not sure what you intended to do, but if you wanted to pass the actual array, just pass arr. If you planned to pass a sub-array, this method is incorrect altogether as you pass an offset inside an array and you will get skewed data.

Hope This helps, Lior

Lior
  • 284
  • 1
  • 6
  • Hey Lior, thanks for your response! So I'm determining the size of the array by taking user input, meaning I can't fully set arrays as parameters unless they're pointers since I can't initialize size. Do you know any way to work around that? Thanks again! – jess Oct 15 '19 at 13:28