递增和用C解引用指针顺序++ [英] Order of incrementing and dereferencing pointer in C++

查看:130
本文介绍了递增和用C解引用指针顺序++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我导师C ++学生和最近遇到涉及与数组名指针算法有问题就来了。我感到困惑的主要事情是语句

  T MIN_VALUE = *开始++;

CPLUSPLUS 告诉我,++运算符具有较高的precedence比在*引用操作,所以我认为开始先增加再解除引用。此外,网站证实,当你通过数组的函数的名称,它被变成指针的第一个元素的地址,元素[0]。然而,当我运行下面的Visual Studio中的code,它看起来像MIN_VALUE被设定为1.5开头,这似乎是矛盾的时候,我觉得操作的顺序是。

我觉得应该是:


  1. 递增开始指针[1]元素(第2数组中)

  2. 取消引用指针值

  3. 设置MIN_VALUE为等于所述阵列中的第二个元素。

不过,我的实验似乎表明,而不是不同的东西正在发生的事情:


  1. 取消引用指针值

  2. 设置MIN_VALUE等于数组元素1

  3. 增量指向下一个元素

有人能澄清这一点?

  //问题3:请写分钟()函数和max()函数的实现..#包括LT&;&iostream的GT;
使用命名空间std;
模板< typename的T>T最小(T *开头,T *完)
{
        ŧMIN_VALUE = *开始++;
        同时(开始!=结束)//你可以使用循环了。
        {
                如果(*&开始LT; MIN_VALUE)
                        MIN_VALUE = *开始;
                开始++;
        }
        返回MIN_VALUE;
}
模板< typename的T>
T最大(T *开头,T *完)
{
        ŧMAX_VALUE = *开始++;
        同时(开始!=结束)
        {
                如果(*&开始GT; MAX_VALUE)
                        MAX_VALUE = *开始;
                开始++;
        }
        返回MAX_VALUE;
}
诠释的main()
{
        双ARR [] = {1.5,4.5,3.5,2.5,5.5};
        int值中[] = {1,2,3,4,-1,5};
        COUT<< 改编的分[]是:<<分钟(ARR,编曲+ 5)LT;< ENDL;
        COUT<< 价值分钟[]是:<<分钟(值,值+ 6)LT;< ENDL;
        COUT<< 改编的最大值[]是:<< MAX(ARR,编曲+ 5)LT;< ENDL;
        COUT<< 值的最大值[]是:<< MAX(值,值+ 6)LT;< ENDL;
}


解决方案

precedence是只为code应该如何解析的规则。 ++ 是第一位的,而 * 排第二。但是,在执行code时,你必须考虑一下实际运营的

在您的情况下,会发生以下情况:


  1. 的副本开始制成。

  2. 原来递增。

  3. 副本被返回。

  4. 副本是取消引用

  5. 拷贝被分配到 MIN_VALUE

这仅仅是递增运算符是如何工作的,这也是你如何写操作,当你重载它自己的类型:

  T ++运算符(INT)
{
    ŧ副本= *这一点;
    ++(*此);
    返回副本;
}


实际上,在的情况下,内置的递增运算符,增量不一定必须是步骤2.它也可以发生在以后,只要观察行为是相同的。例如,没有什么可以阻止从递增的原始值已经返回了复制后的编译器。在你自己的,重载操作符,你可以不执行,当然,这样的事情,

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:

  1. increment the begin pointer to the [1] element (2nd in the array)
  2. dereference the pointer value
  3. 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:

  1. dereference pointer value
  2. set min_value equal to 1st element of array
  3. 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; 
}

解决方案

Precedence is only a rule for how the code should be parsed. ++ comes first, and * comes second. But when the code is executed, you have to consider what the operators actually do.

In your case, the following happens:

  1. A copy of begin is made.
  2. The original is incremented.
  3. The copy is returned.
  4. The copy is dereferenced.
  5. The copy is assigned to min_value.

That's just how the post-increment operator works, and it's also how you write the operator when you overload it for your own types:

T operator++(int)
{
    T copy = *this;
    ++(*this);
    return copy;
}


Actually, in the case of the built-in post-increment operator, incrementation does not necessarily have to be step 2. It could also happen at a later point, as long as the observable behaviour is the same. For example, nothing stops the compiler from incrementing the original value after it has returned the copy. You could not perform such a thing in your own, overloaded operator, of course.

这篇关于递增和用C解引用指针顺序++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆