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

Answer to Exercises 3.37 is not entirely true. #198

Open
glucu opened this issue Mar 12, 2021 · 0 comments
Open

Answer to Exercises 3.37 is not entirely true. #198

glucu opened this issue Mar 12, 2021 · 0 comments

Comments

@glucu
Copy link

glucu commented Mar 12, 2021

Exercise 3.37

Question or Bug

What does the following program do?

const char ca[] = {'h', 'e', 'l', 'l', 'o'};
const char *cp = ca;
while (*cp) {
    cout << *cp << endl;
    ++cp;
}

It's written: Print all the elements of the array. However, there is no null terminator that has a stopping criteria, so it ends up printing all the elements in ca, but continues to traverse past the last element, resulting in a undefined behavior. It should be:

const char ca[] = {'h', 'e', 'l', 'l', 'o', '\0'};
const char *cp = ca;
while (*cp) {
    cout << *cp << endl;
    ++cp;
}

Your enviroment information

  • System: NA
  • Compiler version/IDE: NA
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