I am practising for a test where there are debugging questions. For the code below it says there are 2 errors inside the function, but it doesn't seem like there is an error plus i tried running it and it does work fine (the code given is intended to look weird but I'm pretty sure it functions correctly). Is there maybe something that will make an undefined behaviour or error in the question?
Question:
The following function takes an integer array and the length of that array. It should calculate the sum of that array and return the computed value. Identify and explain the two errors in this function and explain how to fix them.
int sum(int nums[], int nums_size){
int *total;
if((total = (int *) malloc(sizeof(int))) == NULL){
exit(1);
}
for(int i = 0 ; i < nums_size; i++){
*total += nums[i];
}
return *total;
}