从函数C ++返回字符数组时出错

时间:2016-04-11 23:06:52

标签: c++ arrays arduino

我正在为Arduino板写一个草图,并使用以下C ++代码。我试图通过在dec2bin函数中传递十进制数来将十进制数转换为二进制数,这将返回一个我将打印出来的字符数组。但是,我收到了错误:

"不相容的' char'的分配类型到' char [0]'"

在对dec2bin的函数调用中,我在dec2bin函数内部返回时收到另一个错误:

"来自' char *'的转换无效去#char;' [-fpermissive]"

如果有人能帮助我,我将不胜感激。我需要在这里使用字符数组而不是字符串!谢谢!

void loop() {
// put your main code here, to run repeatedly:
 if (Serial.available() > 0){
    char BinaryNum [0];
    int Decimal = Serial.parseInt();
    BinaryNum = dec2bin(Decimal);
    Serial.println (BinaryNum);

 }

}

char dec2bin (int Decimal){

  int Remainder; // Remainder of Decimal%2
  char Binary [0]; // Character array returned by dec2bin
  int x = 0;

  while (Decimal != 0 ){
    Remainder = Decimal%2; 
    Decimal = Decimal/2;
    Binary[x] = Remainder; 
    Serial.println(Binary[x]);
    x+=1;
  }

  return Binary;
}

2 个答案:

答案 0 :(得分:1)

Danger Danger, you're returning a pointer to a local variable, Binary, that is on the stack, once the function returns it is out of scope and no longer valid. This will cause weirdness, it will work some times and then it will stop working, don't do it!

See Can a local variable's memory be accessed outside its scope?

You need to pass in the storage.

eg.

void loop() {
// put your main code here, to run repeatedly:
 if (Serial.available() > 0){
    char BinaryNum[33]; // allows for 32 bits plus null terminator
    int Decimal = Serial.parseInt();
    dec2bin(Decimal, BinaryNum);
    Serial.println (BinaryNum);

 }

}

void dec2bin (int Decimal, char* Binary){

  int Remainder; // Remainder of Decimal%2
  int x = 0;

  while (Decimal != 0 ){
    Remainder = Decimal%2; 
    Decimal = Decimal/2;
    Binary[x] = Remainder; 
    x+=1;
  }
  Binary[x] = '\0';
}

On a "real" computer with an operating system there are many more memory management options than on a little arduino. You could allocation from the heap and use something fancy like an auto_ptr to manage it (see What is a smart pointer and when should I use one?).

This just isn't possible on something as small as an arduino. You could however allocate it as a static. This isn't appropriate for a fancy computer because it is not re-entrant and thus not thread safe. Static allocation is done at program linking or startup and persistent across calls.

void loop() {
// put your main code here, to run repeatedly:
 if (Serial.available() > 0){
    int Decimal = Serial.parseInt();
    char* BinaryNum  = dec2bin(Decimal);
    Serial.println (BinaryNum);

 }

}

void dec2bin (int Decimal) {
  static Binary[33]; // allows for 32 bits plus null terminator

  int Remainder; // Remainder of Decimal%2
  int x = 0;

  while (Decimal != 0 ){
    Remainder = Decimal%2; 
    Decimal = Decimal/2;
    Binary[x] = Remainder; 
    x+=1;
  }
  Binary[x] = '\0';

  return Binary;
}

答案 1 :(得分:0)

我将假设Serial.println()接受nul-terminated char*

dec2bin的签名应该是char* dec2bin(int decimal),其中char*是char数组开头的pointer

然后你需要声明并分配char* binary = new char[arraySize];。请注意,无法重新调整数组大小。 (不要忘记稍后用delete[])释放这段记忆。您可能还想将'\0'添加到数组的末尾。

如果要返回数组本身而不是指针(不推荐),请参阅此post