C++ 如何使用strcpy将字符串复制到char数组中

C++ 如何使用strcpy将字符串复制到char数组中,c++,c,string,arduino,C++,C,String,Arduino,我试图将值复制到字符中 我的字符数组是 char sms_phone_number[15]; 顺便问一下,你能告诉我是否应该写(有什么好处/区别?) 下面显示一个字符串:“+417611142356” 我想把这个值从你的号码给短信 // strcpy(sms_from_number,splitedString[1]); // OP's statement strcpy(sms_phone_number,splitedString[1]); // edit 我有一个错误,我想是因为s

我试图将值复制到字符中

我的字符数组是

char sms_phone_number[15];
顺便问一下,你能告诉我是否应该写(有什么好处/区别?)

下面显示一个字符串:“+417611142356”

我想把这个值从你的号码给短信

// strcpy(sms_from_number,splitedString[1]);   // OP's statement 
strcpy(sms_phone_number,splitedString[1]);  // edit 
我有一个错误,我想是因为splitedString[1]是一个字符串,不是吗

sim908\u cooking:835:错误:从“char”到“char*”的转换无效

那么我怎样才能正确地复制它呢。 我也尝试了sprintf,但没有成功

非常感谢你的帮助。
干杯

我宣布像这样挥霍

// SlitString
#define NBVALS 9
char *splitedString[NBVALS];
我有这个功能 分割线(“托托,+345,提提”,分割线)

我知道,我对char和pointer*:o(

声明:

  char * SplittedString[15];
声明指向字符的指针数组,也称为C样式字符串

鉴于:

  const char phone1[] = "(555) 853-1212";
  const char phone2[] = "(818) 161-0000";
  const char phone3[] = "+01242648883";
您可以将它们分配给您的
SplittedString
数组:

  SplittedString[0] = phone1;
  SplittedString[1] = phone2;
  SplittedString[2] = phone3;
为了给你更多的帮助,以上作业应该是:

  SplittedString[0] = &phone1[0];
  SplittedString[1] = &phone2[0];
  SplittedString[2] = &phone3[0];
根据定义,
SplittedStrings
数组包含指向单个字符的指针,因此最后一组赋值是正确的版本

如果允许,请将
std::string
优先于
char*
,将
std::vector
优先于数组

您需要的是字符串向量:

std::vector<std::string> SplittedStrings(15);
或动态分配的字符串:

  char *spliedString = new char [256];

字符串和字符可能会让noob感到困惑,特别是如果您使用过其他更灵活的语言

char msg[40];  // creates an array 40 long that can contains characters
msg = 'a';     // this gives an error as 'a' is not 40 characters long 
(void) strcpy(msg, "a");              // but is fine : "a"
(void) strcat(msg, "b");              // and this    : "ab"
(void) sprintf(msg,"%s%c",msg, 'c');  // and this    : "abc"

HTH

拆分字符串的类型是什么?看起来像是一个字符*到了
char*sms\U电话号码的程度[15]
,这是一个由15个不同的
字符组成的数组*
。因此它可以用作字符串的集合,但前提是您将成员初始化为某个值,否则它们只会指向未定义的行为。您好,splitedString是一个字符。splitedString[1]包含电话号码,如+41761111222。splitedString[2]包含另一个文本,等等,这些都很混乱。也许你应该发布整个代码,而不仅仅是片段,这样我们就可以看到声明。-1,不清楚你的问题是什么如果你感到困惑,研究StackOverflow的一些例子,使用这些关键字:“c++数组c样式字符串”
  SplittedString[0] = phone1;
  SplittedString[1] = phone2;
  SplittedString[2] = phone3;
  SplittedString[0] = &phone1[0];
  SplittedString[1] = &phone2[0];
  SplittedString[2] = &phone3[0];
std::vector<std::string> SplittedStrings(15);
  char spliedString[256];  
  char *spliedString = new char [256];
char msg[40];  // creates an array 40 long that can contains characters
msg = 'a';     // this gives an error as 'a' is not 40 characters long 
(void) strcpy(msg, "a");              // but is fine : "a"
(void) strcat(msg, "b");              // and this    : "ab"
(void) sprintf(msg,"%s%c",msg, 'c');  // and this    : "abc"