Struct - Variable String Length

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

    testLength : BYTE;
    testName : STRING[testLength];

    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?



    JAPIB - 2020-06-29

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


  • nothinrandom

    nothinrandom - 2020-06-29


    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.