如何在C中跟踪结构成员值的变化?

时间:2011-05-12 01:08:47

标签: c tracking

struct STATE{
  uint8 bit;
  uint8 cop;
}
STATE *state_array[1024];

state_aray[0]->bit = 8;     
state_aray[0].cop =  8;
state_aray[1]->bit = 0;     
state_aray[1].cop = state_aray[1]->bit & 8 != state_aray[0]->bit & 8
state_aray[2]->bit = 12;   
state_aray[2].cop = state_aray[1]->bit & 8 != state_aray[0]->bit & 8 && state_aray[2]->bit & 8 != state_aray[1]->bit & 8
state_aray[3]->bit = 0;     
state_aray[3].cop = state_aray[1]->bit & 8 != state_aray[0]->bit & 8 && state_aray[2]->bit & 8 != state_aray[1]->bit & 8 && state_aray[3]->bit & 8 != state_aray[2]->bit & 8
state_aray[4]->bit = 8;     
state_aray[4].cop = ...

state_aray[5].cop = ... ...
...
state_aray[100].cop = ... ...... ......... ....... ......... ....... ...... ....... ............... ........ ...... ............ .... ......... ...... ..



有没有办法只使用state_aray [i-1] - > cop和state_aray [i-1] - > bit和state_aray [i] - > bit来获取state_aray [i] - > cop ?

TKS

1 个答案:

答案 0 :(得分:0)

不行。在C中没有“我是这种结构的成员”机制,而不是OOP的“这个”或“自我”概念。 这就是为什么在C中你只是隐藏你的结构实现并提供函数来改变你偷偷设置其他值的值。

STATE *create_state(void)
{
    STATE *retval;

    if( (retval = malloc(sizeof(STATE))) == NULL )
        return NULL;
    retval->cop = -1;
    retval->bit = 0;
    return retval;
}

void set_state(STATE *st, int val)
{
    st->bit = val;
    st->cop++;
}

int has_state_changed(STATE *st)
{
    return st->cop;
}