Hi, this is my first post here and I'm a beginner, so please be gentle!
I was writing some code that involved passing an array to a class method and I noticed that the method was able to modify the array, even if it was of the type void. This doesn't happen when other variable types such as int are passed. I think this is because a pointer to the first element is passed rather than the array itself, so it's like passing a variable by reference. In my code I actually don't want the array used as the parameter to be modified outside of the method, is there any way to achieve this other than making a copy of the array before passing?
How would I adjust the following code to stop the array getting modified by mod_array()? Is it even possible?
Thanks Chervil but what I want is for the array to be modifiable within the method mod_array() but for the original array to be unaffected. Using const means I can't modify the array within mod_array().
This example is trivial so maybe I should say how I encountered this problem. As an exercise I am making a poker game. The players 7 cards (2 hole and 5 community) are stored in an array called rank_array, which holds values from 2 to 14 (where 12 is a queen, 14 is an Ace). This array is passed to a method of another class that sorts it numerically then works out if the player has a pair or a straight etc. But I don't want the original array to be sorted because I use it later in another class to display the cards.
If you do that, then I would still use the const keyword, it both documents your intentions, as well as getting the compiler to check that your code is complying with what you told it you wanted to do.