更新时间:2023-02-03 16:02:28
使用
int* salary_pointer = new int[20];
相反,当您分配20个int
时,不只是一个.然后,使用
instead, as you allocate 20 int
s, not just one. Then, delete the dynamic array using
delete[] salary_pointer;
而不是delete salary_pointer
.这里也要小心:
instead of delete salary_pointer
. Be also careful here:
salary[i] / (i + 1);
如果操作数为int
,则最终会被截断.如果希望结果作为double
,请使用salary[i]/(i + 1.)
(在这种情况下,***将salary
做成double
的数组或指向double
的指针,这样就不再有此问题了).
If the operands are int
, then you end up with truncation. Use salary[i]/(i + 1.)
in case you want your result as a double
(in which case you better make salary
an array of double
s or a pointer to double
so you don't have this issue anymore).