0

I need to send a value saved in a variable. When trying to pass this in my payload it keeps referencing to the initial value of mydata, which is unsuccessful test.

    static uint8_t *mydatapnt;
    uint8_t *strdata = "Successfull test";
    static uint8_t mydata [] = "Incorrect test";   

    void send(){
      if (LMIC.opmode & OP_TXRXPEND) {
      Serial.println(F("OP_TXRXPEND, not sending"));
      } else {
         // Prepare upstream data transmission at the next possible time.
         LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
         Serial.println(F("Packet queued"));
    /
    }

    void setup(){
     *mydata = mydatapnt;
    }

    void loop(){
     *mydatapnt = strdata;
     Serial.println(*mydatapnt + "");
    }

What exactly am I doing wrong? I should receive "successfull test". You can find the full code project here

Maartenw
  • 595
  • 1
  • 5
  • 19

2 Answers2

2

About strings...

Strings are just pieces of memory where characters are stored (an array of characters). Strings are terminated by a null character.

To copy a string, you must have sufficient memory in the target string. Then you use strcpy to copy the characters. strcpy appends the terminating null character.

The size of a string is determined with strlen. It does not count the terminating null charcter.

In

char *strptr = "my string";

the pointer points to the literal string. A literal string is read-only. Trying to modify it often leads to a segmentation fault or other terminal error. In:

char myString[] = "my string";

The characters of "my string" are copied to the array myString and the compiler makes its size of the length of the initializing string, plus a null character.

To append to a string, you use strcat. Again, the target must have sufficient memory. You either declare a char array with enough memory, like char mystr[100]; or you can allocate memory with malloc, for example:

char *strptr= malloc(strlen(myString)+1);
strcpy(strptr, myString);

If you no longer need the memory you got from malloc, you must return it to the memory allocator:

free(strptr);

Trying to use strptr after having freed the memory will also result in a segmentation fault or in some other undefined behavior.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • Maarten, the hints are clear enough that you can find that out yourself. There are many possibilities; you have to choose one. Try it, and come back if you have problems. – Paul Ogilvie Jan 15 '18 at 14:04
1

*mydatapnt = "success in loop";

This does not store pointer to "success in loop" string into mydatapnt, but rather store address of that string into first character of mydatapnt. (At this time mydatapnt is not initialized, so it clobbered some random memory location.)

What you more likely wanted to do is: mydatapnt = "success in loop";

Serial.println(*mydatapnt + "");

You're not concatenating strings, but adding value of first char from mydatapnt to pointer to empty string, which is more like substring operation. This will likely print something, because of the way strings are stored in executable, but definetely not what you wanted it to print.

You can't concatenate strings in C with simple +. Try just Serial.println(mydatapnt).

flapenguin
  • 380
  • 1
  • 3
  • 16