如何在C中使用char*

ase*_*_le 0 c c++

我有这样的主要功能:

void main()
{
     char *s;
     inputString(s);
     printf("%s",s);

}
Run Code Online (Sandbox Code Playgroud)

和inputString函数:

void inputString(char *&s)
{

    //Some code line to input a string and set s point to this string

}
Run Code Online (Sandbox Code Playgroud)

是否有一个函数自动malloc内存足够存储字符串输入(我需要在inputString函数中输入字符串).

Pra*_*rav 5

只需3行代码(将这些代码放入其中int main())就足够了

std::string s;
std::cin >> s; //or getline() as desired
std::cout << s;
Run Code Online (Sandbox Code Playgroud)