c++ - 如何在 C 中使用 char *

标签 c++ c

我有这样的主要功能:

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

}

和 inputString 函数:

void inputString(char *&s)
{

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

}

是否有一个函数auto malloc memory enough store string which input(我需要在inputString函数中输入字符串)。

最佳答案

只需 3 行代码(将它们放在 int main() 中)就足够了

std::string s;
std::cin >> s; //or getline() as desired
std::cout << s;

关于c++ - 如何在 C 中使用 char *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4378411/

相关文章:

c - 尝试更改 char* 指针位置时出现 "Error: expression must be a modifiable lvalue"

c++ - 两个 "using"可以碰撞吗?

c++ - 如何使用动态转换检测到指针的删除

c++ - 多重继承 : same variable name

c++ - 另一种语言的 Tesseract setVariable 白名单

c - fscanf 读入双数组奇怪值

c++ - 在 C++ 中用不完整的派生类覆盖方法返回类型

c++ - 在 x86_64 上非临时加载 32 位和 64 位值的 C/C++ 内在函数?

连接 5 个或更多字节并转换为十进制,然后转换为 ASCII

c - [] 和 * 的位置