c++ 6. (b hint : just rewrite the while loop condition) Consider the following d
ID: 3910340 • Letter: C
Question
c++
6. (b hint : just rewrite the while loop condition) Consider the following declaration of a C-string variable, where SIzE is a def ned constant: char ourstring [SIZE] The C-string variable ourstring has been assigned in code not shown here. For correct C-string variables, the following loop reassigns all positions o the value »x', leaving the length the same as before. Assume this code fragment is embedded in an otherwise complete and correct program. Answer the questions following this code fragment. int index0 f ourstring while (ourString [index] != '') ourstring(index) x index++i Explain how this code can destroy the contents of memory beyond the end of the array. a. b. Modify this loop to protect against inadvertently changing memory beyond the end of the array. Give at least three uses of the operator. Name and describe each useExplanation / Answer
The problem will be in the case when the number of char in string are equal to SIZE then there is no memory for '' character hence the loop will not terminate and probably overwrite the contents of memory that it was not meant to.
b)
The proper implementation can be
while(index<SIZE)
{
outString[index]='X';
index++;
}
Do give a thumbs up and in case there are doubts leave a comment.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.