1
_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??

1
  • Please decide. Is this question about C or is it about C++? Commented Jan 11, 2015 at 15:44

1 Answer 1

7

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';
}
Sign up to request clarification or add additional context in comments.

2 Comments

am i correct to assume that i would make char results[4] global so it can be accessed/assigned outside loop?
@Simon No, you should just declare it in the same scope of the loop. And if you want it in some other function, just pass it as a pointer to char *, and it cannot be assigned anywhere, you can assign to it's elements though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.