This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TMS320F28335: Struct upcasting and offsetof macro

Part Number: TMS320F28335


Tool/software: Code Composer Studio

Hi everybody,

in my application I am using this compounded structure:

typedef struct binary_i binary_i_t; 

struct binary_i {
    binary_t binaryClass;   /*!< Binary class >*/
    QFsm super[NUMBER_OF_BI];           /*!< QFsm super class >*/
    bi_ctl_t biCtlQFsm[NUMBER_OF_BI];   /*!< Binary input control register >*/
};

In the code there is a function, which launches binary scanning finite state machine:
BinaryFSM_Process(QFsm *const me) {
binary_i_t *bme = ((char*)me - offsetof(binary_i_t, super[FSM_number]));
....
}

  Mentioned function is invoked by this way:

BinaryFSM_Process(&BIN_scanner.biFSM[FSM_number]);

In this function I need to upcast me pointer to binary_i_t structure. Firstly, I have tried to solve it in Code::Block on my PC - with offsetof macro and (char*) upcasting, please,  see this piece of code:

binary_i_t *bme = ((char*)me - offsetof(binary_i_t, super[FSM_number]));

It has been working correctly, in accordance with my wishes.

However, when I attempted to realize it in CCS6, this error was appeared: #28: expression must have a constant value. In my opinion, probably due to variable FSM_number.


At the moment I don't know, how to fix it. Is there any other method to upcast me pointer to binary_i_t structure, for exaple without offsetof macro? Why CCS6 generates error mentioned above?

Thank you a lot.

 

  • Hello,

    I will see if an expert of the C2000 compiler can explain the usage of offsetof() macro or if they have other suggestions to solve the casting. Thanks for your patience.

    Regards,
    Elizabeth
  • Miroslav Martisek said:
    when I attempted to realize it in CCS6, this error was appeared: #28: expression must have a constant value. In my opinion, probably due to variable FSM_number.

    That is correct.  Using offsetof in that way violates the standard.  The second argument to offsetof can only be the name of a field in the structure, not an expression which evaluates at runtime.  See section 7.17 of the C99 ANSI standard for C.  I can only guess the compiler on your PC accepts an expression as some sort of extension to the language.

    Thanks and regards,

    -George