且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

作业:使用指针制作数组

更新时间:2023-02-03 16:02:28

使用

int* salary_pointer = new int[20];

相反,当您分配20个int时,不只是一个.然后,使用

instead, as you allocate 20 ints, 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 doubles or a pointer to double so you don't have this issue anymore).