The suggested solution can store an erroneous value in largest or cause undefined behaviour.
Problem:
Incrementing p right after checking p < a+n causes a problem after p is incremented to point to the last element in the array: a+(n-1). After the iteration completes, the condition is checked and because a+(n-1) < a+n is true, the loop proceeds to increment p to point to a + n (out of bounds of the array) and continue the loop, potentially causing undefined behaviour.
Suggested solution:
Change p++ to ++p.
The suggested solution can store an erroneous value in
largestor cause undefined behaviour.Problem:
Incrementing
pright after checkingp < a+ncauses a problem afterpis incremented to point to the last element in the array:a+(n-1). After the iteration completes, the condition is checked and becausea+(n-1) < a+nis true, the loop proceeds to incrementpto point toa + n(out of bounds of the array) and continue the loop, potentially causing undefined behaviour.Suggested solution:
Change
p++to++p.