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

发布于 2024-11-06 19:44:57 字数 1092 浏览 1 评论 0原文

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

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?

tks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2024-11-13 19:44:57

行不通。与 OOP 的“this”或“self”概念相反,C 中不存在“我是该结构的成员”机制。
这就是为什么在 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;
}

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文