【问题标题】:How to copy a string into a char array with strcpy如何使用 strcpy 将字符串复制到 char 数组中
【发布时间】:2014-11-14 09:53:00
【问题描述】:

我试图将一个值复制到一个字符中。

我的字符数组是

char sms_phone_number[15];

顺便问一下,能不能告诉我要不要写(有什么好处/区别?)

char * sms_phone_number[15]

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

splitedString[1]

我想将该值赋予 sms_from_number

// strcpy(sms_from_number,splitedString[1]);   // OP's statement 
strcpy(sms_phone_number,splitedString[1]);  // edit 

我有一个错误,我认为是因为 splitedString[1] 是一个字符串,不是吗?

sim908_cooking:835:错误:从 'char' 到 'char*' 的无效转换

那么我怎样才能正确地复制它。 我也尝试使用 sprintf 没有成功。

非常感谢您的帮助。 干杯

【问题讨论】:

  • splitedString 的类型是什么?看起来是char*
  • char * sms_phone_number[15],即15个不同char *的数组。因此它可以用作字符串的集合,但前提是您将成员初始化为某个值,否则它们只会将您指向未定义的行为。
  • 您好,splitedString 是一个字符。 splitedString[1] 包含一个电话号码,例如 +41761111222。 splitedString[2] 包含另一个文本等
  • 这一切都很混乱。也许您应该发布整个代码,而不仅仅是 sn-ps,以便我们可以看到声明。
  • -1,不清楚你的问题是什么

标签: c++ c string arduino


【解决方案1】:

我这样声明 spliedString

// SlitString
#define NBVALS 9
char *splitedString[NBVALS];

我有这个功能 splitString("toto,+345,titi",slitedString)

void splitString(char *ligne, char **splitedString)
{

  char *p = ligne;

  int i = 0;
  splitedString[i++] = p;
  while (*p) {


    if (*p==',') {
      *p++ = '\0';
      if (i<NBVALS){
         splitedString[i++] = p;
      }
    } 
    else
    {
      p++;
    }

  }
  while(i<NBVALS){
    splitedString[i++] = p; 
  }
}

如果我用 splitedString 显示 for,它会显示这个

for(int i=0;i<4;i++){
Serialprint(i);Serial.print(":");Serial.println(splitedString[i]);
}

//0:toto
//1:+4176112233
//2:14/09/19

我也声明并想复制..

char sms_who[15];
char sms_phone_number[15];
char sms_data[15];
//and I want to copy 
strcpy(sms_who,splitedString[0]
strcpy(sms_phone_number,splitedString[1]
strcpy(sms_date,splitedString[2]

我知道,我很困惑 char 和指针 * :o(

【讨论】:

  • 如果你感到困惑,研究 StackOverflow 的一些例子,使用这些关键字:“c++ array c-style string”
【解决方案2】:

声明:

  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);

编辑 1:

提醒:为您的spliedString 分配空间。

您的spliedString 应该是一个预先分配的数组:

  char spliedString[256];  

或动态分配的字符串:

  char *spliedString = new char [256];

【讨论】:

    【解决方案3】:

    字符串和字符可能会让新手感到困惑,尤其是当您使用其他更灵活的语言时。

    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

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多