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
3 Réponses
+ 2
num + i access the next address and then use the '*' to put the value read by cin at that adress
+ 2
Since you're creating the array dynamically you need to explicitly free memory at the end.
delete [] nums
+ 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 :)