Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 12 Exercise 12 - pointer increments to point outside the array #127

Open
ShaunakRawat opened this issue Feb 19, 2024 · 0 comments
Open

Comments

@ShaunakRawat
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant