c++初学者一枚,希望有好心人能够帮我解答这行代码的意思

该题为c++ primer plus 书籍中 第七章课后习题

这个double * fill_array(double *pfirst, double *pend)函数体中,
最后的return 返回了一个 --ptemp; 为什么要反回这个,这个位置有点看不懂,反回--ptemp是反回的什么东西?指针地址的自减吗?
另外: if (!cin) // bad input
{

cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated.\n";
if(i == 0)
return ptemp;
else
return --ptemp;
}

这里也有 return --ptemp; 希望能有朋友帮我解答疑惑,感激不尽,为何要反回这个呢!

#include
const int Max = 5;

// function prototypes
double * fill_array(double *pfirst, double *pend);
void show_array( double *pfirst, double *pend);
void revalue(double *pfirst, double *pend, double r);

int main()
{
using namespace std;
double properties[Max];

double  *pos = fill_array(properties, properties+(Max-1));
show_array(properties, properties+(Max-1));
cout << "Enter revaluation factor: ";
double factor;
cin >> factor;
revalue(properties, properties+(Max-1), factor);
show_array(properties, properties+(Max-1));
cout << "Done.\n";
return 0;

}

double * fill_array(double *pfirst, double *pend)
{
using namespace std;
double temp;
double * ptemp;
int i = 0;

for (ptemp = pfirst; ptemp <= pend; ptemp++)
{
    cout << "Enter value #" << (i + 1) << ": ";
    cin >> temp;

    if (!cin)    // bad input
    {   
        cin.clear();
        while (cin.get() != '\n')
            continue;
       cout << "Bad input; input process terminated.\n";
       if(i == 0)
           return ptemp;
       else 
           return --ptemp;
    }
    else if (temp < 0)     // signal to terminate
        break;
    pfirst[i] = temp;
    i++;
}
return --ptemp;

}

// the following function can use, but not alter,
// the array whose address is pfirst
void show_array( double *pfirst, double *pend)
{
using namespace std;
double * ptemp;
int i = 0;
for (ptemp = pfirst; ptemp <= pend; ptemp++,i++)
{
cout << "Property #" << (i + 1) << ": $";
cout << *ptemp << endl;
}
}

// multiplies each element of ptemp by r
void revalue(double *pfirst, double *pend, double r)
{
double *ptemp;
for (ptemp = pfirst; ptemp <= pend; ptemp++)
*ptemp *= r;
}

就你的代码来说
return --ptemp;

return ptemp - 1;
没有区别
这里返回的是数组最后一个元素的地址。

用 --ptemp是因为for循环每次会将ptemp++,那么在最后一次循环后,ptemp指针实际指向最后一个有效地址的下一个地址。
估计函数要求返回的是最后一个有效地址,所以需要循环结束后,将ptemp向后回退一个地址,才是有效的地址。

-- 运算符在前,先计算再返回。如果 -- 运算符在后,就没有意义了:先返回再减一。

运算符的话嘛 嗯 没错

--ptemp;代表着ptemp - 1,刚才那么些运行快些,

###如果lz的疑问是:--ptemp 是什么东西?

假如ptemp指向a[7],则--ptemp指向a[7]之前的一个元素,既a[6]


###如果lz的疑问是:--ptemp 放在这里的意义是个啥?

我脑子笨,暂时没有很好的解释。倒是可以通过返回的指针判断是否全部输入正确。:)

for里有个 ptemp++
跳出循环的时候,ptemp指向的位置是下一个位置,所以要返回它指向的前一个位置 即 ptemp - 1; 也即 -- ptemp;

注意,不能是 ptemp --;