About 465,000 results
Open links in new tab
  1. Returning a C string from a function - Stack Overflow

    Mar 19, 2019 · There are two ways to return strings that won't barf so readily. returning buffers (static or dynamically allocated) that live for a while. In C++ use 'helper classes' (for example, std::string) to handle the longevity of data (which requires changing the function's return value), or; pass a buffer to the function that gets filled in with ...

  2. How to return a string from a C++ function? - Stack Overflow

    May 18, 2013 · std::string::find returns a std::string::size_type (aka. size_t ) not an int . Two differences: size_t is unsigned, and it's not necessarily the same size as an int depending on your platform (eg. on 64 bits Linux or Windows size_t is unsigned 64 bits while int is signed 32 bits).

  3. How to Return a String From a Function in C++ - Delft Stack

    Feb 12, 2024 · One of the simplest ways to return a string from a function is by returning it as a value. This involves creating a local string variable within the function, populating it with the desired content, and then returning it. Return by the value is the preferred method for returning string objects from functions.

  4. c++ - best way to return an std::string that local to a function ...

    Oct 20, 2010 · Try to step debug into a function that returns std::string and examine the innards of that object that is about to be return. You shall see a member field pointer address xxx . And then, examine the std::string variable that received the function's return value.

  5. Returning Strings from Functions in C++ – An In-Depth Guide

    We‘ve explored various ways to return string values from functions in C++, along with pros, cons, use cases, and recommendations for each approach. While C++ offers flexibility, default to using return by value for its simplicity, safety, and efficiency thanks to move semantics.

  6. Return String in C++: A Quick and Simple Guide

    Discover how to return string in C++ with ease. This concise guide covers key techniques and examples to master string handling effortlessly. In C++, you can return a string from a function by specifying the return type as `std::string` and using the `return` statement to send the desired string value back to the caller. Here's a simple example:

  7. String Functions In C++ - GeeksforGeeks

    Oct 31, 2023 · Use the substr () function to extract a substring from a string. 6. The find () function returns the position of the first occurrence of a substring. 7. Use the replace () function to modify a part of the string. The insert () function adds a substring at a specified position. Use the erase () function to remove a part of the string. 8.

  8. C++ Functions - Return - W3Schools

    If you want the function to return a value, you can use a data type (such as int, string, etc.) instead of void, and use the return keyword inside the function: This example returns the sum of a function with two parameters: You can also store the result in a variable:

  9. Proper way to return a String from a function? : r/C_Programming - Reddit

    Jun 5, 2015 · Pass a pointer to a string buffer and a length as parameters to your function, use those to return your string (using strncpy to make sure you don't overflow your buffer). Allocate the returned string using malloc and have the caller be responsible for freeing it.

  10. c++ - How to return a string literal from a function - Stack Overflow

    Mar 20, 2010 · char* func3 {return "yet another string literal";} will not compile at all. Nor will. char* func4 {return &"a ref to a string literal?";} Stroustrup says in "The C++ Programming Language" (Third Edition): "A string literal is statically allocated so that it is safe to return one from a function.

  11. Some results have been removed