You don't need the pointer sign and the subscript operator. (Oh wait I see, you're doing an MD array)
In that case you should read this article which describes why you should avoid md arrays and how to pass them: http://www.cplusplus.com/forum/articles/17108/
Thx for the reply.
Perhaps this seems an MD array, but this is only a example. I must try to solve this as practice. The main passed a array of pointers to the FillArr function. This function point each element of the recived array to a array of int. The main function then calls the PrintArr function to show the content of the int arrays.
When i call the PrintArr function from in the FillArr function the output is correct. But when i call the PrintArr function from the main the output isn't correct.
But when i show the pointers (p[0], p[1], p[2]) of the pointer array self in the FIllArr function and in the main. The are on both place the same.
In main(), p is declared to be an array of 3 pointers to ints. (This is how the 2D array is actually stored in memory)
In FillArray(), you assign those three pointers to be pointers to the arrays declared locally on lines 23, 24, 25.
Those three local arrays are declared on the stack. They go away when FillArr returns, which means p has pointers
to unallocated stack memory.
I do not fully understand what this means, if I may still just trying to understand.
I put after line 29 in FillArr , cout << "Address pt[0] = " << pt[0] << endl;
and after line 7 in main following code, cout << "Address p[0] = " << p[0] << endl;
and in both case the output is 0x22fee0
To my option this maen that i have in main the correct address to the location where Arr1 is stored. But this is a address on the stack and the content of this address is lost by leaving the FillArr function. If this isn't what you maen please correct my.
Maybe a last question, why if i call the PrintArr function after line 29 of the FillArr function the output is corect?
And finally, is there a way by using a pointer array to solve this assignment, or should I look a completely different way to find the sollution, and cane you give me a tip.
To my option this maen that i have in main the correct address to the location where Arr1 is stored. But this is a address on the stack and the content of this address is lost by leaving the FillArr function. If this isn't what you maen please correct my.
That's correct.
Maybe a last question, why if i call the PrintArr function after line 29 of the FillArr function the output is corect?
Because the value hasn't been overwritten yet. Since it is free memory, it can be overwritten; it just so happens in
your case it wasn't. You are relying on undefined behavior.
And finally, is there a way by using a pointer array to solve this assignment, or should I look a completely different way to find the sollution, and cane you give me a tip.