This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C51bugs

This code generate 2 bugs:

#define BUG_TEST 0

typedef char * PCHAR;

typedef struct {
    int I;
    int J;
} A_OLD;

typedef struct {
    int nI;
    int nJ;
} A, *PA;
typedef A xdata * PAx;

A_OLD cAOld;

#if     BUG_TEST == 0   //Normal
int l1 = (PCHAR)&(*(PA)(&cAOld));                                               //Get address of struct
int l2 = ((PCHAR)&((*(PA)(&cAOld)).nI) - (PCHAR)&(*(PA)(&cAOld)));              //Calc offset of member
#elif   BUG_TEST == 1   //Cann't initialize
int l1 = (PCHAR)&(*(PAx)(&cAOld));                                              //Get address of struct
#elif   BUG_TEST == 2   //General fault C51
int l2 = (unsigned int)((PCHAR)&((*(PA)(&cAOld)).nI) - (PCHAR)&(*(PA)(&cAOld)));//Calc offset of member
#endif

Parents
  • I frequently have the same problem. Here is an example of a header file that I use for generating IDs of text strings that are array subscripts to a table of text messages (which may need translation to other languages). The same method can be used with 'offsetof' to generate a table of indexes.

    //  +--------------------------------------------------------------------------+
    //  |   TxtTable.h                                                             |
    //  |                                                                          |
    //  |   If "InModTxtTable" is defined, initializations are performed. To use   |
    //  |   this feature, "include" the file twice.  The first time before the     |
    //  |   definition of "InModTxtTable", the second time after the definition.   |
    //  |                                                                          |
    //  +--------------------------------------------------------------------------+
    
    
    #ifdef  _txttableh
        #ifdef  InModTxtTable
            #undef _txttableh
        #endif
    #endif
    
    #ifndef _txttableh
    #define _txttableh
    
    #ifdef  InModTxtTable
        #define TxtItem(Id, Text) Text
    
        code char code *TxtString[] = {
    #else
        #define TxtItem(Id, Text) Id
    
        enum TextStringIds {
    #endif
    
    
    /* --  Text Strings for language conversion  -------------------------------- */
    
    TxtItem ( tidOpErrBase,     "NoneDetected"      ),
    TxtItem ( tidMemFailure,    "RAM Failure"       ),
    TxtItem ( tidSigFailure,    "Params Reset"      ),
    TxtItem ( tidE2MemoryErr,   "E2 Mem Fail"       ),
    TxtItem ( tidLocalHBusErr,  "Loc Bus Fail"      ),
    TxtItem ( tidSystemHBusErr, "Sys Bus Fail"      ),
    
    /*----------------------------------------------------------------------------*/
    
    #ifdef InModTxtTable
        (void *)0
    #else
        MaxTextStringId
    #endif
    };
    
    #undef  TxtItem
    
    #endif
    

Reply
  • I frequently have the same problem. Here is an example of a header file that I use for generating IDs of text strings that are array subscripts to a table of text messages (which may need translation to other languages). The same method can be used with 'offsetof' to generate a table of indexes.

    //  +--------------------------------------------------------------------------+
    //  |   TxtTable.h                                                             |
    //  |                                                                          |
    //  |   If "InModTxtTable" is defined, initializations are performed. To use   |
    //  |   this feature, "include" the file twice.  The first time before the     |
    //  |   definition of "InModTxtTable", the second time after the definition.   |
    //  |                                                                          |
    //  +--------------------------------------------------------------------------+
    
    
    #ifdef  _txttableh
        #ifdef  InModTxtTable
            #undef _txttableh
        #endif
    #endif
    
    #ifndef _txttableh
    #define _txttableh
    
    #ifdef  InModTxtTable
        #define TxtItem(Id, Text) Text
    
        code char code *TxtString[] = {
    #else
        #define TxtItem(Id, Text) Id
    
        enum TextStringIds {
    #endif
    
    
    /* --  Text Strings for language conversion  -------------------------------- */
    
    TxtItem ( tidOpErrBase,     "NoneDetected"      ),
    TxtItem ( tidMemFailure,    "RAM Failure"       ),
    TxtItem ( tidSigFailure,    "Params Reset"      ),
    TxtItem ( tidE2MemoryErr,   "E2 Mem Fail"       ),
    TxtItem ( tidLocalHBusErr,  "Loc Bus Fail"      ),
    TxtItem ( tidSystemHBusErr, "Sys Bus Fail"      ),
    
    /*----------------------------------------------------------------------------*/
    
    #ifdef InModTxtTable
        (void *)0
    #else
        MaxTextStringId
    #endif
    };
    
    #undef  TxtItem
    
    #endif
    

Children
No data