Error: Invalid conversion from 'char * to' char

Please bear with me, I am new to C and I am trying to program an Arduino. I want to write a program that spits out a data frame of a specific length with byte values ​​from 0 to 255. The minimum code to reproduce the error is below. I am getting the following error when compiling:

sketch_apr09b.cpp: In function ‘char assembleFrame()’:
sketch_apr09b.cpp:9:10: error: invalid conversion fromchar*’ to ‘char
      

Now I get the impression that I am mishandling the "bounce frame", but I just can't figure out what happened.

char assembleFrame() {
  char frame[] = { 
    0x61 , 0x62 , 0x63
  };
  return frame;
}

void setup() {
  Serial.begin( 115200 );
};

void loop() {
  char frame = assembleFrame();
  Serial.print( frame );
}

      

When I run hexdump on the receiving PC, I want to see:

00000000  61 62 63                                          |abc|
00000003

      

I found many similar questions, couldn't figure out what I was doing wrong.

EDIT : This is what I came up with, but I'm getting the wrong data. I think I am sending a pointer to the actual data with this.

byte *assembleFrame() {
  byte frame[] = { 4 , 'a' , 'b' , 'c' };
  return frame;
}

void setup() {
  Serial.begin( 115200 );
};

void loop() {
  byte *frame = assembleFrame();
  Serial.write( frame , frame[ 0 ] );
}

      

+3


source to share


3 answers


The type is char

used to store one char

(byte).

The function definition char assembleFrame

states that the function will only return one char

, so when trying to return an array / pointer char[]

/ char *

( char

) it doesn't work.



It looks like it Serial.print()

can handle char *

that potentially needs to be null terminated (since it asks for a length specifier).

char *assembleFrame() {
  char frame[] = {
    0x61, 0x62, 0x63, 0x00 // null byte to signify end of string
  };
  return frame;
}

void setup() {
  Serial.begin( 115200 );
};

void loop() {
  char *frame = assembleFrame();
  Serial.print( frame );
}

      

+3


source


Looking at this code:

char frame = assembleFrame();

      



Considering how you use it later, it is clear that frame

to be declared char *

not char

, and assembleFrame

must be declared as returning char *

.

0


source


assembleFrame

returns char

as long as you return char*

. Since I don't know what you are doing, I cannot suggest how to fix this. Perhaps the correct return type is correct.

-1


source







All Articles