Struct - Variable String Length

2020-06-29
2020-06-29
  • nothinrandom

    nothinrandom - 2020-06-29

    I would like to create a struct that has variable string length. However, CoDeSys compiler is not happy since testLength is not a constant.

    STRUCT
    testLength : BYTE;
    testName : STRING[testLength];
    END_STRUCT

    I have been reading through documentation but could not find an answer to this, so I hope it is not impossible. Maybe there is another way to approach this?

    Thanks,

     
  • JAPIB

    JAPIB - 2020-06-29

    Hi,
    It seems that the length of a chain must be defined only by a constant.

    So you can only write : testName : STRING[10];
    And not :
    testLength : BYTE:=10; //Even if we initialize the value
    testName : STRING[testLength]; //Not OK

    Even in the case of a variable declaration (not a structure),

    BR

     
  • nothinrandom

    nothinrandom - 2020-06-29

    @JAPIB,

    Thanks for confirming! I am a bit bummed out since this would make packing/unpacking structs not straight forward at all. But I guess the alternative would create possibility of buffer overflow, so it is not safe. Now, I need to really think about how to unpack structs with this limitation.

     

Log in to post a comment.