DMike92
DMike92

Reputation: 23

Does cstring variables needs to be static to work with ESP8266?

Despite 5 years of development with ESP8266 (mini D1) I'l still confused: My IDE version is 1.8.19 My sketch is 3300 lines but I made a test case: This is the sketch:

// Globals
int i=1;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(1000);
  Serial.println();
  Serial.println("Small test...");
  Serial.println("Case 1");
}

void test() {
  char* essai = "Hello my wolrd";    // (1) This does not ré-assign essai to the value each time !
  // static char essai[50] = {0};    // (2) This works static or not !!!!!!
  // char* essai = "Hello my wolrd"; // (3) This does not work as expected
  // strcpy(essai, "Hello my wolrd");// (4)
  Serial.printf("essai 1='%s'\n",essai);
  strcpy(essai,"Hello your wolrd"); // your world
  Serial.printf("essai 2='%s'\n",essai);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (i < 4) {  // i.e. 3 times
    Serial.printf("Test %d...\n",i);
    test();
  }
  if (i == 4) {
    Serial.println("Done test");
  }
  i++;
  delay(1000);
}

I expected the resuls would print three times 2 different lines but see: the results:

    Small test...
    Case 1 active line: 1
    Test 1...
    essai 1='Hello my wolrd'
    essai 2='Hello your wolrd'
    Test 2...
    essai 1='Hello your wolrd'
    essai 2='d'
    Test 3...
    essai 1='d'
    essai 2='d'
    Done test
    
    Small test...
    Case 2 active lines: 1,4
    Test 1...
    essai 1='Hello my wolrd'
    essai 2='Hello your wolrd'
    Test 2...
    essai 1='Hello your wolrd'
    essai 2='d'
    Test 3...
    essai 1='d'
    essai 2='d'
    Done test
    
    Small test...
    Case 3 active line: 2
    Test 1...
    essai 1=''
    essai 2='Hello your wolrd'
    Test 2...
    essai 1='Hello your wolrd'
    essai 2='Hello your wolrd'
    Test 3...
    essai 1='Hello your wolrd'
    essai 2='Hello your wolrd'
    Done test
    
    Small test...
    Case 4  active lines: 2,4 ==> result as expected
    Test 1...
    essai 1='Hello my wolrd'
    essai 2='Hello your wolrd'
    Test 2...
    essai 1='Hello my wolrd'
    essai 2='Hello your wolrd'
    Test 3...
    essai 1='Hello my wolrd'
    essai 2='Hello your wolrd'
    Done test
    
    
    Small test...
    Case 5 active line: 3
    Test 1...
    essai 1='Hello my wolrd'
    essai 2='Hello your wolrd'
    Test 2...
    essai 1='Hello your wolrd'
    essai 2='d'
    Test 3...
    essai 1='d'
    essai 2='d'
    Done test
    
    Small test...
    Case 6 active lines: 3,4 
    Test 1...
    essai 1='Hello my wolrd'
    essai 2='Hello your wolrd'
    Test 2...
    essai 1='Hello your wolrd'
    essai 2='d'
    Test 3...
    essai 1='d'
    essai 2='d'
    Done test
    
    Small test...
    Case 7  active lines: 2 (line 2 not anymore static)  result as expected
    Test 1...
    essai 1='Hello my wolrd'
    essai 2='Hello your wolrd'
    Test 2...
    essai 1='Hello my wolrd'
    essai 2='Hello your wolrd'
    Test 3...
    essai 1='Hello my wolrd'
    essai 2='Hello your wolrd'
    Done test

Is that normal that

char* essai="Hello my world"; 

does not work as expected (by me!)? (Sorry for mispelling World).

Upvotes: 0

Views: 24

Answers (0)

Related Questions