Cubbi, Andy, Vlad:
Thank you so much for the information and advice. I've learned a lot, and I've always been a bit baffled at proper syntax for sending a ptr to a 2D array, so thank you.
To answer some questions:
You want to send the values individually?? |
I worded that part ambiguously -- oops. I meant, "By doing it this way, am I pushing the entire array onto the stack? Because I'd like to avoid that if possible (i.e. I'd highly prefer if the only thing sent through the stack was one pointer/address when that function is called once.)
By the way, your orig. question is a bit ambiguous when it comes to what you want? |
Also, I may be able to clear something up by saying that the size of the two dimensions would also be passed into the function, so that it would support multiple sized 2D arrays. So something like: ShowArray(map, 8, 5) or ShowArray(table, 6, 31). I hard-coded the 8's because I was more concerned with the passing the ptr properly and figured I'd cross the bridge of having variable sizes when I come to it. As to exactly what I want, the function, in reality, is a SaveFunc which (hopefully) takes a ptr to the 2D array (which may be different x,y, and writes it to a binary file (which is already implemented and working).
Vlad, thank you for the suggestion, but, if possible, I would like to use the most basic-as-possible datatypes, mostly for myself, to have a project that would be a good reference for bare foundations work. (I'm even using char arrays instead of strings so that I never forget how to implement them without issue)
-
So, with the addition of wanting variable sized dimensions, is it still possible? How could I use the vars passed in, in the proper places? I almost feel that I would have to ... cast after the fact? Am I wrong?
1 2 3 4 5 6 7 8 9 10 11 12
|
ShowArray(int* ptr, int x, int y)
{
int (*arr)[][x] = &ptr;
for(int row = 0; row < y; ++row)
{
for(int col = 0; col < x; ++col)
{
func(arr[row][col]);
}
}
}
|