简体   繁体   English

在struct中设置数组值会在struct中设置另一个字段

[英]Setting array value in struct sets a different field in struct

I have a piece of code as follows, and when the line with comment (//error here) executes, it sets the status variable instead of the element in the array. 我有一段代码,如下所示,当执行带注释的行(此处为// error)时,它将设置状态变量,而不是数组中的元素。

I moved the int status element above the array_name element in the struct definition and that seemed to have fixed it, I suspect that I am changing a pointer and am missing some parenthesis, but i am not sure why this was happening. 我将int status元素移到了结构定义中的array_name元素上方,并且似乎已经修复了该问题,我怀疑我正在更改指针并缺少括号,但是我不确定为什么会这样。

#define MAX_NUM 20
typedef struct FOO_T {
    bool abc;
    int id;
    int array_name[MAX_NUM];
    int counter;
    int status;
    SYSTEMTIME timestamp;

    struct FOO_T *next;
}

if (curr->array_name[code] == 0 )
{
    curr->counter++;
    curr->array_name[code] =  curr->counter; //error here
}

I also initialize the whole struct in a different function, part of that function is this: 我还用另一个函数初始化了整个结构,该函数的一部分是这样的:

thing->id = 0;
for (i = 0; i < MAX_NUM; i++) thing->array_name[i] = 0;
thing->counter = 0;
thing->status = 0;

You should check that code is less than MAX_NUM . 您应检查code是否少于MAX_NUM

In your particular case code is probably MAX_NUM + 1 在您的情况下, code可能是MAX_NUM + 1

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

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