Simon.
Simon.

Reputation: 1896

arduino, function to return char array

_10_11.ino: In function 'void loop()':
_10_11:73: error: initializer fails to determine size of 'results'
_10_11.ino: In function 'char getData()':
_10_11:160: error: invalid operands of types 'const char*' and 'const char [5]' to binary 'operator+'


In short, i have a function char getData() which returns char output[50] = "1: " + cmOne + " 2: " + cmTwo + " 3: " + cmThree + " 4: " + cmFour; where int cmOne, cmTwo, cmThree, cmFour.

In loop, i call:

char results[] = getData();

    client.println("1: %i", results[0]);
    client.println("2: %i", results[1]);
    client.println("3: %i", results[2]);
    client.println("4: %i", results[3]);

I know that i'm wrong with my data types, assigning etc but am abit off with how to do it best, any suggestions??

Upvotes: 1

Views: 21791

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53026

That's not possible, create a fixed size array, and pass it to the function as a pointer, and initialize it in the function

char results[4];

getData(results); /* the getData function should take a 'char *' paramenter */

client.println("1: %i", results[0]);
client.println("2: %i", results[1]);
client.println("3: %i", results[2]);
client.println("4: %i", results[3]);

and of course if the array is bigger just char results[A_BIGGER_SIZE];

Suppose that get data just puts a string "ABC" in the result array it would look like

void getData(char *dest)
{
    dest[0] = 'A';
    dest[1] = 'B';
    dest[2] = 'C';
    dest[3] = '\0';
}

Upvotes: 7

Related Questions