I tutor students in C++, and recently came across a problem involving pointer arithmetic with array names. The main thing I'm confused about is the statement
T min_value = *begin++;
Cplusplus tells me that the ++ operator has higher precedence than the * dereference operator, so I assume that begin is first incremented and then dereferenced. Also, this site confirms that when you pass the name of an array to a function, it gets turned into a pointer to the address of the first element, element [0]. However, when I run the code below in Visual Studio, it looks like min_value is set to be 1.5 at the beginning, which seems to contradict what I think the order of operations are.
I think it should be:
- increment the begin pointer to the [1] element (2nd in the array)
- dereference the pointer value
- set min_value to be equal to the 2nd element in the array.
However, my experiment seems to indicate that instead something different is happening:
- dereference pointer value
- set min_value equal to 1st element of array
- increment pointer to next element
Can someone clarify this?
// Problem #3: Please write the implementation of min() function and max() function..
#include <iostream>
using namespace std;
template<typename T>
T min(T* begin, T* end)
{
T min_value = *begin++;
while(begin != end) // You can use for-loop too.
{
if( *begin < min_value)
min_value = *begin;
begin++;
}
return min_value;
}
template<typename T>
T max(T* begin, T* end)
{
T max_value = *begin++;
while(begin != end)
{
if( *begin > max_value)
max_value = *begin;
begin++;
}
return max_value;
}
int main()
{
double arr[] = { 1.5, 4.5, 3.5, 2.5, 5.5 };
int values[] = { 1, 2, 3, 4, -1, 5 };
cout << "min of arr[] is : " << min(arr, arr + 5) << endl;
cout << "min of values[] is : " << min(values, values + 6) << endl;
cout << "max of arr[] is : " << max(arr, arr + 5) << endl;
cout << "max of values[] is : " << max(values, values + 6) << endl;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…