简体   繁体   English

如何跟踪C中的struct成员值更改?

[英]how to track a struct member value change in C?

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 = ... ...... ......... ....... ......... ....... ...... ....... ............... ........ ...... ............ .... ......... ...... ..


is there a way to only use state_aray[i-1]->cop and state_aray[i-1]->bit and state_aray[i]->bit to get state_aray[i]->cop? 有没有办法只使用state_aray [i-1]-> cop和state_aray [i-1]-> bit和state_aray [i]-> bit来获得state_aray [i]-> cop?

tks ks

Won't work. 不行 There's no "I'm a member of this structure" mechanism in C, as opposed to OOP's "this" or "self" concepts. 与OOP的“ this”或“ self”概念相反,C中没有“我是此结构的成员”机制。 That's why in C you simply hide your structure implementation and provide functions to change the values in which you sneakily set other values. 这就是为什么在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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM