Mundane Question

I've been doing some experimenting with multidimensional arrays, and my goal has been to create an array in (main), pass it to a function, have the function alter it, and then have the altered array able to be further manipulated in main. I scoured the internet and I found it very easy to find tutorials on how to pass arrays, both single and multidimensional, into functions, but literally nothing on how to change the array by reference.

After MUCH poking and prodding around, I figured out something that works, but I'm not sure why it works. I finished coding this little program in order to illustrate what I'm trying to do.

#include <iostream>

using namespace std;

int *ReverseArray(int *orig, int b)
{
int swap;
int c = -1;
for(int a=-4; a < -2; a++)
{
swap = orig[a];
orig[a]=orig[c];
orig[c]=swap;
c--;
}
return orig;
}

int main()
{
const unsigned short int SIZE = 2;
int ARRAY[SIZE][SIZE] = {{1,2},{3,4}};
int *arr = ARRAY[SIZE];

for(int i = 0; i < 2 * SIZE; i++)
cout << arr[i-2*SIZE] << " ";

cout << std::endl << endl;
arr = ReverseArray(arr, SIZE);
cout << endl;
for(int i = 0; i < 2 * SIZE; i++)
std::cout << arr[i- 2 * SIZE] << " ";

cout << std::endl;

return 0;
}

When run, this program couts "1 2 3 4" the function calls, the array is passed the function, and the function replaces "1 2 3 4" with "4 3 2 1". This is great, perfect, awesome, but my question has to do with the pointer passed to the function.

int *arr = ARRAY[SIZE];

for(int i = 0; i < 2 * SIZE; i++)
cout << arr[i-2*SIZE] << " ";

When assigning my multidimensional array ARRAY to my pointer arr, after much trial and error I found that despite the fact that I assigned arr to point at the first element in ARRAY, in order for my program to run correctly, i had to shift arr[i-4]. My question is why? Obvious 4 is the total number of elements in the multidimensional array, but it seems that my point is pointing not to the first element in ARRAY but rather the first element after ARRAY ends. Obviously, I can go through my C++ career simply shifting all of my array pointers over by the total size of the multidimensional array, but I'm looking for a cleaner and easier way to do this. I'm still rather pleased I even got the program working, but I'm just looking for a bit of help.

Thanks in advance.
closed account (j3bk4iN6)
1
2
3
4
5
6
7
for(int a=-4; a < -2; a++)
{ 
        swap = orig[a];
        orig[a]=orig[c];
        orig[c]=swap;
        c--;
}


maybe it has something to do with how you initialize loop at -4.
No, I had to initialize the loop at -4 in order to get it to work. My question is why?
closed account (j3bk4iN6)
Maybe because when you iterated in the loop your range might have been off. try using sizeof(). I would try for(int i = 0; i < range + 1; i++) or some derivitive. You have to play with it.

I got it to work with

1
2
3
4
5
6
for(int i = 0; i < 3; i++)
{ 
swap = orig[i];
orig[i]=orig[c];
orig[c]=swap;
c++;


but you have to fix some things because the copy misses the 4th number
Last edited on
I appreciate the help, but the loop was just incidental. I finished the program in order to be able to post something here that actually compiles, but the weird off-shift of -4 happens before the function is even called.

1
2
3
4
5
6
const unsigned short int SIZE = 2;
int ARRAY[SIZE][SIZE] = {{1,2},{3,4}};
int *arr = ARRAY[SIZE];

for(int i = 0; i < 2 * SIZE; i++)
cout << arr[i-2*SIZE] << " ";


Right there in (main) is where I found I had to offset arr by -4, or 2*SIZE. This was before it was even passed to the function. Using arr[0] didnt work, and arr[0][0] didnt work because arr[i] is pointing to an array.

So before the function is even called, the -4 offset is necessary. The program doesn't work without it, but I just don't understand why since I've pointed arr at ARRAY[SIZE] that it needs a -4 dereferencer.

The program's order is:
cout 1 2 3 4
call function
cout 4 3 2 1

The arr[-4] shift has to happen before the function is even called, so the function can't be responsible. I could continue coding two dimensional arrays like this for the rest of my days, but I'm looking for the correct way. Thanks.
Last edited on
that's the way I had to do it in order to get it to compile. I don't want to have to build all my programs around my multidimensional array size *2 lol. I'm very novice, I was tinkering with these multidimensional arrays all day yesterday and i finally got it to just compile, nothing else.
closed account (j3bk4iN6)
all i can say is if your index is iterating up from -4 then the array[-4] will be your first value instead of array[0]. I am very much a novice, so mine is all speculation. If you notice in my bio I don't include C++ as one of my languages yet
Last edited on
Topic archived. No new replies allowed.