The following C++ function is supposed to return a valid pointer to a string, but actually has a bug. string* greeting() { string* s = new string("Hi!"); return s; }
Added by Jordan D.
Step 1
The code is wrong because it returns a pointer to a local variable, which is the string s. The variable s is created on the stack when the function is called, and it is destroyed when the function returns. Show more…
Show all steps
Your feedback will help us improve your experience
Fatina Seck and 80 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Write a function whose prototype is char lastChar(const char *str) that takes a nonempty C-string as a parameter and returns the last character in the string. For example, the call lastChar("abc") will return the character 'c'.
Sarvesh S.
Output of the following program is (A) compilation error (B) hello (C) print null string (D) no output
Programming and Data Structures
Functions
Write a function in C++ that returns the count of a string's suffix in a given string, S. A suffix is a combination of one or more symbols appended at the end of the string. You will be given the length, L, of the suffix as a parameter to the function. Constraints: - S does not contain newline characters - Length(S) >= L > 0 Template: ```cpp #include <iostream> #include <string> int suffixCount(std::string S, int L) { // Your code here } ``` Sample Input: ``` et tu, brute 1 ``` Sample Output: ``` 2 ```
Supreeta N.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Watch the video solution with this free unlock.
EMAIL
PASSWORD