0

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).

3
  • the main problem is that the second sentence is longer. I guess you get some compilation warnings. best practice is to declare constants with const Commented Sep 27, 2024 at 6:38
  • Good point. I'll try with 'your' first an see if the problem remains. I'll comme when done Commented Sep 28, 2024 at 11:37
  • @Juraj Ok, using char essai[50] = "any text"; fix the problem. But I dont want to declare it const beacause I need to change it. Anyway problem solved Thank you Juraj :) Do I have to close this topic? How? Commented Sep 28, 2024 at 22:54

0

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.