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;

void setup() {

void loop() {
        // put your main code here, to run repeatedly:
  • While commenting on an answer to a previous question of yours, I suggested you read the section Arrays and Pointers of the C FAQ. Judging from from this new questions, it seems you failed to do so. Please, read it! Otherwise you will spend lots of time asking here related (and off-topic) questions. Commented Aug 22, 2018 at 10:39
  • @EdgarBonet well I did read it. I must say that the main purpose or motivation of declaring a char as char* - is quite unclear
    – guyd
    Commented Aug 22, 2018 at 10:41
  • To be clear- I understand the technicalities of such definitions, but not when or why to use each one of them. It is a whole different from Python
    – guyd
    Commented Aug 22, 2018 at 10:45

1 Answer 1


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.

  • thank you. There's something I'm missing : 1) In any other case if I would init char *test1="hello world" at the beginning, and print it Serial.println(test1) - I would have get proper value. 2) In code here - when I create test inside it does print it as expected, but not passing its value to test2 ?
    – guyd
    Commented Aug 22, 2018 at 6:48
  • char *test1="hello world" would be a pointer to a global allocated constant c-string and should be const char *test1="hello world". I enhanced the answer.
    – Juraj
    Commented Aug 22, 2018 at 6:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.