How to create and return string from function?

how to return a string from a function in c++
how to return a string in c
c function return char array
return string pointer from function in c
how to return a string python
how a function can return a string
how to get a function to return a string
c create string in function

Would like to generate a string from a function, in order to format some data, so the function should return a string.

Tried to do the "obvious", shown below, but this prints garbage:

#include <iostream>
#include <string>

char * hello_world()
{
    char res[13];
    memcpy(res, "Hello world\n", 13);
    return res;
}

int main(void)
{
    printf(hello_world());
    return 0;
}

I think this is because the memory on the stack used for the res variable, defined in the function, is overwritten before the value can be written, maybe when the printf call uses the stack.

If I move char res[13]; outside the function, thus makes it global, then it works.

So is the answer to have a global char buffer (string) that can be used for the result?

Maybe doing something like:

char * hello_world(char * res)
{
    memcpy(res, "Hello world\n", 13);  // 11 characters + newline + 0 for string termination
    return res;
}

char res[13];

int main(void)
{
    printf(hello_world(res));
    return 0;
}

You are programming c. That's not bad, but your question is about c++ so this is the solution for the question you asked:

std::string hello_world()
{
    std::string temp;

    // todo: do whatever string operations you want here
    temp = "Hello World";

    return temp;
}

int main()
{
    std::string result = hello_world();

    std::cout << result << std::endl;

    return 0;
}

How to create and return string from function?, You are programming c. That's not bad, but your question is about c++ so this is the solution for the question you asked: std::string hello_world()  Simply declare the function as a char * which would allow you to return a pointer to the desired string. It's been a long time since I've had to return a string from a function so I believe you'd have to either declare the string inside the function as a static char or use dynamic memory allocation in order

Don't bother with that early-20th century stuff. By the end of the previous century we already had std::string, and that's straightforward:

#include <iostream>
#include <string>

std::string hello_world()
{
    return "Hello world\n";
}

int main()
{
    std::cout << hello_world();
}

Returning C string from a function, There are two ways to return strings that won't barf so readily. You can create the array in the caller, which is the main function, and pass the  To return a string from a JavaScript function, use the return statement in JavaScript.ExampleYou need to run the following code to learn how to return a string

Best solution would be to use std::string. However, if you must use an array, then it is best to allocate it in the calling function (in this case, main()):

#include <iostream>
#include <cstring>

void hello_world(char * s)
{
    memcpy(s, "Hello world\n", 13);
}

int main(void)
{
    char mys[13];
    hello_world(mys);
    std::cout<<mys;
    return 0;
}

Good ways to return string from function in C - Lynx Chang, Don't over-design, make simple things too complicated, but reduce the ease of maintenance and corresponding changes. Staying flexible and  Allocate memory on the heap using malloc or similar, then return that pointer. The caller will then need to call free when it is done with the memory. Allocate the string on the stack in the calling function (the one that will be using the string), and pass a pointer in to the function to put the string into.

Still, if you want to write a pure C code, will can do something like that.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *HelloWorld(char *s, int size)
{
        sprintf(s, "Hello world!\n");
        return s;
}

int main (int argc, char *argv[])
{
        char s[100];

        printf(HelloWorld(s, 100));

        return 0;
}

Return String from a function, is it possible to return a string from a C function (such as an array: x[10])? structure type, but make sure the function is returning a pointer to the  String is an array of characters. In this guide, we learn how to declare strings, how to work with strings in C programming and how to use the pre-defined string handling functions. We will see how to compare two strings, concatenate strings, copy one string to another & perform various string manipulation operations.

How to pass a string as an argument to a function and return a string , Passing a string to a function We pass the address of the character array( string ) to the function, also due to this whatever changes we make to this string in our  In this post I will explain how to pass and return array from function in C programming. Functions makes our program modular and maintainable. Learn C programming, Data Structures tutorials, exercises, examples, programs, hacks, tips and tricks online.

Function return values, Objective: To understand function return values, and how to make use of "The weather is warm" // the replace() string function takes a string,  RETURNS varchar(100) Full code: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create FUNCTION NameFunction ( @eid int ) RETURNS varchar(100) -- or whatever length you need AS BEGIN Declare @logid varchar(50); SELECT @logid = E.LoginId from HumanResources.Employee As E where E.BusinessEntityID = @eid RETURN @logid END GO

Python Tutorial: Functions, Previous Chapter: Formatted output with string modulo and the format method A return statement ends the execution of the function call and "returns" the result, i.e. the Using keyword parameters is an alternative way to make function calls. Working with strings in C can be quite tricky and, without research, you might get confused as why your program is crashing.

Comments
  • use std::string?
  • Or you can add using namespace std. It is the C++ Standard Library which is a collection of classes and functions. The built in C++ library routines are kept in the standard namespace. That includes stuff like cout, cin, string, vector, map, etc.
  • Thanks for the suggestion; accepted the other answer, since it is closer to the actual application.
  • What is the memset for? It doesn't matter. Also, sprintf will cause a buffer overflow if size is too small.
  • You are absolutely right, it there for no reason. Let me edit it.