0

I used the internet to complete a c++ practice task and I understand everything except for one line, line 13. Please help.

Why do I have to insert *(nums+i) in line 13 and not *nums only? Plus is there a need for delete[ ]nums in this code? https://code.sololearn.com/chF2CrH3QTaV/?ref=app

25th May 2021, 7:02 PM
Anthony Asiimwe
Anthony Asiimwe - avatar
3 Answers
+ 2
num + i access the next address and then use the '*' to put the value read by cin at that adress
25th May 2021, 7:19 PM
Alpha Diallo
Alpha Diallo - avatar
+ 2
Since you're creating the array dynamically you need to explicitly free memory at the end. delete [] nums
25th May 2021, 7:54 PM
Alpha Diallo
Alpha Diallo - avatar
+ 2
*(nums+i)=nums[i]; We write it as nums[i] because it looks simpler but what the compiler is doing is adding i to nums which is the address of the first element of the array and the * accesses the value stored on that address.. Always use delete [] nums(or any other array name) if you have a dynamically allocated memory. So yes, there is a need for delete []nums. Hope this helps :)
25th May 2021, 8:01 PM
Mihail
Mihail - avatar