Arduino错误:无法将参数'1'的'String'转换为'char *'到'char * strtok(char *,const char *)'

时间:2018-06-21 11:45:25

标签: string arduino strtok

我正在处理一个arduino分配,该分配将传入的字符串拆分并将该字符串的项放入6个不同的变量中(拆分后的示例输入字符串具有6个项)。我弹出以下错误:无法将参数'1'的'String'转换为'char *'到'char * strtok(char *,const char *)'。你们可以建议对代码进行更改以使其正确吗?

String str(" ");
char a[5],b[5],c[5],d[5],e[5],f[5];
char *token = strtok(str, " ");


void setup()
{
Serial.begin(9600);

 }
void loop(){
while (!Serial.available());
str = Serial.readStringUntil('\n');
Serial.println(str);

strcpy(a,token);

token = strtok(NULL, " ");
strcpy(b,token);
token = strtok(NULL, " ");
strcpy(c,token);
token = strtok(NULL, " ");
strcpy(d,token);
token = strtok(NULL, " ");
strcpy(e,token);
token = strtok(NULL, " ");
strcpy(f,token);

Serial.println(a);
Serial.println(b);
Serial.println(c);
Serial.println(d);
Serial.println(e);
Serial.println(f);
}

enter code here

2 个答案:

答案 0 :(得分:1)

您需要将str转换为char数组,因为strtok需要char*参数,而不是String

String str("...");
char str_array[str.length()];
str.toCharArray(str_array, str.length());
char* token = strtok(str, " ");

答案 1 :(得分:1)

// Define 
String str = "This is my string"; 

// Length (with one extra character for the null terminator)
int str_len = str.length() + 1; 

// Prepare the character array (the buffer) 
char char_array[str_len];

// Copy it over 
str.toCharArray(char_array, str_len);