C++ 要一个字符*

C++ 要一个字符*,c++,pointers,C++,Pointers,有没有办法做到这一点: void test(char *userInput){ //code } char userInput = "test"; test(userInput); 我有一个错误:进程已完成,退出代码为139,因此如何继续 有没有办法做到这一点: 当然,只需稍微更改一下代码: void test(const char *userInput){ //code } int main() { const char* userInput = "test"

有没有办法做到这一点:

void test(char *userInput){ 
  //code
} 
char userInput = "test"; 
test(userInput); 
我有一个错误:
进程已完成,退出代码为139
,因此如何继续

有没有办法做到这一点:

当然,只需稍微更改一下代码:

void test(const char *userInput){ 
   //code
} 

int main() {
    const char* userInput = "test"; 

    test(userInput); 
}
我有一个错误:
进程已完成,退出代码为139
,因此如何继续


我很确定编译器在这本书出现之前已经给你展示了一些。先修复这些问题。

你把这个标记为C++问题…字符串非常容易使用

// NOTE: userinput can be either std::string OR const char*
void test(std::string userinput)
{
   // code, perhaps echo testing input
   std::cout << userinput << std::endl;
}

// test 290 - invoked somewhere in main
int t290(void)
{
   std::string userInput = "test1";
   test(userInput);

   // some times you can save some typing
   test("test2");     // function declared as above
                      // accepts both const char* or std::string

   return (0);
}
//注意:userinput可以是std::string或const char*
无效测试(标准::字符串用户输入)
{
//代码,可能是回波测试输入

STD::正如其他人指出的,在代码中有一个错误,<代码> char 应该改为<代码> const char */COD>。char只保存一个值,而不是整个字符串,如果函数具有const值,字符串也必须如此。

<代码> char用户输入=“test”;这可能不是你想写的。考虑<代码> char用户输入[][test ];< /Cord>请提供一个最小的、完整的、可验证的例子()。喜欢<代码> STD::String < /C> > >代码> char */COD>。