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

C251 memory pointer problem

Hi,
Is this a valid construct in you Keil C-251 compiler?

unsigned char data buf[20];

typedef struct {
int i;
int j;
} TEST_T;

#define GetStruct() (*(TEST_T *)&buf[2])

void Test(void) {
GetStruct().i = 3;
}

Notice that buf is in the data area and I'm type casting it to a TEST_T struct in the default memory area (which is in far). V3.53 of the compiler compiles buggy code when similar scheme is used. If I change the buf memory area to "near", or if I change the macro to

#define GetStruct() (*(TEST_T data *)&buf[2])

or

#define GetStruct() (*(TEST_T *)(unsigned char *)&buf[2])


then it compiles good code. Question is, was there something fundamentaly wrong with what I did in the macro?

Andy

0