0

how to get string from other function?
i tried this , but come error...
i using Visual Basic C++....

void main()
{
   char name[51];
   name=NAME();
}

int NAME()
{
   char names[51];
   names="john";
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Tom Gunn
1

C++ has two kinds of strings. The strings inherited from C are char arrays with a terminating '\0' character. All of the rules for arrays apply, which means you can only return a pointer to the array somehow. But if the array is local to the function returning it, you are returning a bogus pointer. Not a good thing. ;)

The strings defined by C++ are class objects of the std::string type. They have copy semantics, so it is much easier to use them than the C type strings. I recommend that you use C++ strings until you start getting into the lower levels of C++ because C type strings are very easy to get wrong:

#include <iostream>
#include <string>

std::string Name()
{
    return "Tom";
}

int main()
{
    std::string name = Name();

    std::cout << "My name is " << name << '\n';
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.