Actually, it does succeed in sorting, and this is perfectly fine code. Array's are nothing more than thinly veiled pointers, so while it is true the function is not technically passing by reference, that would be because it is passing by address instead.
You can just as easily use this code:
As this code:Code:template<typename T> void selectSort(T arr[], int n) {
- ZekeCode:template<typename T> void selectSort(T *arr, int n) {
Thanks for the info.
So when you added the pointer operator before the array name, you were able to remove the array operator? Why? I tried it out but I don't quite see why that still works.
Arrays are pointers, that's why. When arrays are declared, they return a pointer to a block sequence of values that are right next to each other, and when you use the array operator ([]), you're essentially asking to dereference the pointer value plus whatever value is inside of the array operator brackets. An example might be better... essentially using the array operator like this:
Is the same as using this:Code:int t = myArray[5];
However since that doesn't really LOOK that good, they opted to use the array operator.Code:int t = *(myArray + 5);
There's one other difference I know of with arrays, and that's when you're using dynamic allocation and the delete operator. You need to use "delete[]" when you delete an array instead of plain old "delete", like you would for data types, structs, and objects.
Hope that helps.
- Zeke
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks