Question
C++ Please read the entire question! A two-dimensional array of characters stores a C-style string as each of its elements. Consider the following code snippet:
C++
Please read the entire question!
A two-dimensional array of characters stores a C-style string as each of its elements. Consider the following code snippet: char *strings[] = new *char[10]; This defines an array strings of 10 character pointers. The character pointers are unitialized and must not be used before they are set to newly allocated memory. Setting the 5th element of the array to a newly allocated C-style string is given below. strings[4] = new char[8]; memcpy_s(strings[4], 8, allocd, 8); Which can be displayed by using the following code segment. cout << This string has been << strings[4] << endl; /* Displays: * This string has been allocd */ On blackboard, a skeleton for lab23.cpp is provided for you. The program is missing a definition for the function addString(). The purpose of the function is to extend the stringList by one newly allocated string. You must complete the implementation without modifying any other portion of the program. A correctly implemented program will produce the following output, without any run-time errors due to incorrect memory access.
############################ lab23.cpp ###################################
#include
/** * Adds a string to the list. * * Since the list is resized, it returns a pointer to * the newly allocated memory. * * Note: stringList will be released by this function, * but the individual strings will not. * * stringList - the current array of character pointers to strings * count - the number of elements in stringList * str - the string being added. */ char** addString(char *stringList[], size_t count, char *str) { /* * The following statement is a place holder, * allowing the program to compile. */ return stringList; }
void printStrings(char *stringList[], size_t count) { cout << "List: "; for (size_t i = 0; i < count; i++) { cout << stringList[i] << " "; } cout << endl; }
int main(void) { char **stringList = new char*[0]; size_t count = 0;
cout << "Echo with History" << endl << "Please enter an integer, it will be echo'd back to you" << endl << "This will continue until you press CTRL-Z and enter" << endl; do { cout << "> "; string user; cin >> user; size_t len = user.length() + 1; char *str = new char[len]; memcpy_s(str, len, user.c_str(), len); stringList = addString(stringList, count, str); count++; printStrings(stringList, count); } while (!cin.fail());
/* Release the individual char* */ for (size_t i = 0; i < count; i++) { delete stringList[i]; } /* Release the array of char* pointers */ delete stringList; return 0; }
############################ Output Example ################################### Echo with History Please enter an integer, it will be echo'd back to you This will continue until you press CTRL-Z and enter > one List: one > 2 List: one 2 > three List: one 2 three > four List: one 2 three four > ^Z List: one 2 three four Press any key to continue . . .
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started