return char array

I want to send a byte into a Function array and return array s but it don't work. my code :

void loop{

byte A; char B[3]; char C[3];; C[0,1,2]=data(A); // send a byte and return char serial.print(c);


char data(byte D){ //function

B[0]="a"; B[1]="b"; B[2]="c"; return B; }

You are at least 100% correct. It don't work! Not like that anyway. Your Errors are too numerous to elaborate on. Since you don't give any specific error message, etc. there is also no question to answer. For any other newbies reading the thread, it always helps to specify (in a language that someone who has no knowledge of what you are trying to do) exactly what is, in your opinion going wrong and whatever suspicions you may have. Stating that it doesn't work is like taking your car to the mechanic and telling him it doesn't run. It may be more helpful to say that it won't start, is making noises, has no power, etc... Arduinos are no different.

When I run the program only "a" char display in serial monitor

a few things to explore:

where is setup()?

C[0,1,2]=data(A);  // send a byte and return char have you ever seen arrays being accessed that way in C++?


is “a” a char or a string? (think what is 'a' versus "a" for the compiler).

 char B[3];
char C[3];
C = B; // <== do you think that in C++ this copies the B array into C?

I would suggest you start going back to the basics of the programming language syntax and capabilities of C and C++.

This code is for example and don’t my program code
((where is setup()? and is “a” a char or a string? (think what is ‘a’ versus “a” for the compiler).)

char B[3];
char C[3];
C = B; // <== do you think that in C++ this copies the B array into C?
Its a function and call in several line some time(C[0,1,2]=data(A); & F[0,1,2]=data(A); & …) .If I write “C = B;” and I can’t use in other line!

Come on - read the C or C++ language definition

In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded, and its side effects are completed before evaluation of the expression E2 begins. The type, value, and value category of the result of the comma expression are exactly the type, value, and value category of the second operand, E2.

So said simply - when you use the comma operator outside a function to separate parameters, it means evaluate each element separately and it returns whatever the last one evaluated to.

Try this code:

int a, b, c;

void setup() {
  a = 1, 2, 3; Serial.println(a); // prints 1 because the comma operator has lower precedence than = 
  a = (1, 2, 3); Serial.println(a); // prints 3
  a = ((b = 8), (c = 3)); Serial.println(a); // initializes  b with 8, c with 3, returns the value of (c=3) thus a with 3,
  a = ((b = 2), (c = 3), 5); Serial.println(a); // initializes  b with 2, c with 3 and a with 5

void loop() {}

So for your codeC[0,1,2]=data(A);
this is what the compiler does with this:

On the left side: evaluate “0,1,2” → based on the above that is 2 so you do C[2] = something

the something is whatever is returned by the right side of the = operator, here a function call, cast into the format of what C holds (here char)

if we look at your function definition [color=blue]char[/color] data(byte D){ you are making a promise to the compiler that this function will returns a char… but in the code you do return B; doesn’t that sounds weird?? The compiler will complain that you are a liar and that you did not keep your promise and give you a warning!

[color=orange]warning: invalid conversion from 'char*' to 'char' [-fpermissive] return B;[/color]

if you ignore that warning the function will convert the value of B - the pointer to (which is the address of) the B array memory space - to a char by taking the least significant byte of that address. if the address is 0x12[color=blue]34[/color] then it will only return


Try this code:

char A[3] = {'1', '2', '3'};
char B[3] = {'1', '2', '3'};

void setup() {
  A[0, 1, 2] = 'X'; 
  Serial.print(A[0]); Serial.print("-"); Serial.print(A[1]); Serial.print("-"); Serial.println(A[2]);
  A[0, 1, 2] = data('X'); 
  Serial.print(A[0]); Serial.print("-"); Serial.print(A[1]); Serial.print("-[0x"); Serial.print(A[2], HEX); Serial.println("]");

char data(byte D) { //function
  B[0] = 'a';
  B[1] = 'b';
  B[2] = 'c';
  Serial.print("Address of B=0x");Serial.println((int)B,HEX); // address of the array in memory will be 0x100 most likely
  return B;
void loop() {}

will get a warning at the compilation and it will print on the console

Address of B=0x1[color=blue]00[/color]

so you can see that

A[0, 1, 2] = 'X'; is the same as A[2] = 'X';
A[0, 1, 2] = data('X'); is the same (ignoring the warning) A[2] = low part of the address of B;

the right way to copy - B into array C (for standard types) is

for (int i = 0; i < 3; i++) C[i] = B[i];// 3 being the nb of items of our array here

We can do different in C++ but I suggest you stick to the for loop for the time being and really really go back to basics of programming. learn the language, the syntax and the grammar otherwise it’s like speaking a foreign language without knowing what you are saying, no-one will understand what you say…

Assuming that your code was anywhere near correct, there is no point in having a function return a global variable. Variables are defined as global so that there is no need to pass them or to return them.

If a function IS to return a local array, then the function must return a pointer to that array. The array must then be protected from going out of scope (by using the static keyword).

This is how I do it:

void setup() {
  while (!Serial) {}  //wait for Serial Monitor to be opened on boards such as Leonardo/Micro/etc.
  char buffer[4];  //create a buffer array large enough to hold the string and terminator
  test(buffer);  //pass the buffer to the function
  Serial.println(buffer);  //buffer now contains the string

void loop() {}

void test(char testBuffer[]) {
  const char string[] = "abc";
  strcpy(testBuffer, string);  //copy the string to the buffer

Please always do Tools > Auto Format on your code before posting it to the forum.

Please always use code tags(</> button on the toolbar) when you post code or error/warning messages.