cenumsstructureundeclared-identifier

undeclared identifier error with a structure in C


I have a structure ST_transaction_t that contains 2 structures, an enumeration and uint32_t members, when I declare the structure ST_transaction account1 I get account1': undeclared identifier error. When I remove the enumeration typed member from the structure it works.

Here is the part of the code with the problem:

typedef struct ST_transaction_t
{
    ST_cardData_t cardHolderData;
    ST_terminalData_t terminalData;
    EN_transState_t transState;
    uint32_t transactionSequenceNumber;
}ST_transaction_t;
typedef enum EN_transState_t
{
    APPROVED, DECLINED_INSUFFECIENT_FUND, DECLINED_STOLEN_CARD, INTERNAL_SERVER_ERROR
}EN_transState_t;

int main() {
ST_transaction_t account1 ;
return 0;
}

Now if did this:

typedef struct ST_transaction_t
{
    ST_cardData_t cardHolderData;
    ST_terminalData_t terminalData;
    //EN_transState_t transState;
    uint32_t transactionSequenceNumber;
}ST_transaction_t;
typedef enum EN_transState_t
{
    APPROVED, DECLINED_INSUFFECIENT_FUND, DECLINED_STOLEN_CARD, INTERNAL_SERVER_ERROR
}EN_transState_t;

int main() {
ST_transaction_t account1 ;
return 0;
}

It works perfectly, so why is that EN_transState_t transState causing that error and how to fix it ?


Solution

  • In your code typedef enum EN_transState_t is only declared after its use in typedef struct ST_transaction_t. In C all types must be declared before it can be referenced. The compiler works from the top down.

    Move the declaration typedef enum EN_transState_t to before typedef struct ST_transaction_tand your code should work.