user48730: I can't figure out why test is not passed properly to test1
#include <Arduino.h>

char *test1;
char *status_reply(char *test) {
        int ans_rel = 1;
        int ans_led = 0;
        int ans_sw = 1;
        sprintf(test,"%d%d%d",ans_rel,ans_sw,ans_led);
        Serial.println(test);
}

void setup() {
        Serial.begin(9600);
        status_reply(test1);
        Serial.println(test1);
}

void loop() {
        // put your main code here, to run repeatedly:
}


user37622: This is not about Arduino. It is C. You have only a pointer to char now. You must allocate memory for the c-string.
char test1[128];
If you pass pointer to sprintf, the function will write to memory where the pointer points. Something somewhere in memory will be overwritten and the following print will print the string, but later the memory will be used for something else and your c-string disappear.

48730: <p>I can't figure out why <code>test</code> is not passed properly to <code>test1</code></p>

<pre><code>#include &lt;Arduino.h&gt;

char *test1;
char *status_reply(char *test) {
        int ans_rel = 1;
        int ans_led = 0;
        int ans_sw = 1;
        sprintf(test,"%d%d%d",ans_rel,ans_sw,ans_led);
        Serial.println(test);
}

void setup() {
        Serial.begin(9600);
        status_reply(test1);
        Serial.println(test1);
}

void loop() {
        // put your main code here, to run repeatedly:
}
</code></pre>
 37622: <p>This is not about Arduino. It is C. You have only a pointer to char now. You must allocate memory for the c-string.</p>

<p><code>char test1[128];</code></p>

<p>If you pass pointer to sprintf, the function will write to memory where the pointer points. Something somewhere in memory will be overwritten and the following print will print the string, but later the memory will be used for something else and your c-string disappear.</p>
