Strange casting problem with tm structure

Posted by egiakoum1984 on Stack Overflow See other posts from Stack Overflow or by egiakoum1984
Published on 2010-03-12T09:39:47Z Indexed on 2010/03/12 9:57 UTC
Read the original article Hit count: 333

Filed under:

I have the following casting problem when my data structure sSpecificData contains a field of type tm:

typedef struct
{
   unsigned char  data[10000];
} sDataBuffer;

typedef struct
{
   int                 m_Id;
   sDataBuffer         m_Data;
} sData;

typedef struct {
   int                       m_value1;
   int                       m_value2;
   tm                        m_Date;
} sSpecificData;

const int SPECIFIC_SVC_DATA_SIZE = sizeof(sSpecificData);

typedef struct {
   int                    m_Id;
   sSpecificData          m_Data;
} sMyData;

int main(void)
{
       sData svc;
       sMyData* ptr1 = (sMyData*) &svc;
       sSpecificData* ptr2;
       ptr2 = (sSpecificData*) &svc.m_Data;
       ptr1->m_Data.m_value1 = 90;
       ptr1->m_Data.m_value2 = 80;
       cout << ptr1->m_Data.m_value1 << endl;
       cout << ptr1->m_Data.m_value2 << endl;
       cout << ptr2->m_value1 << endl;
       cout << ptr2->m_value2 << endl;       
       return 0;
}

Without the field "tm m_Date;" as part of the sSpecificData, the output is correct:

90
80
90
80

With the field "tm m_Date;" as part of the sSpecificData, the output is wrong:

90
80
0   <-- !
90  <-- !

Is there any idea why my example doesn't work when there is field of type tm as part of the sSpecificData?

Thanks!

© Stack Overflow or respective owner

Related posts about c