0

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.

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.

#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;
5
  • 2
    maybe code is > then MAX_NUM? You just manged to copy not enough code... Commented Feb 25, 2011 at 0:08
  • I added to code above but it was already in my code #define MAX_NUM 20 Commented Feb 25, 2011 at 0:19
  • @rene - It will also break if code == MAX_NUM Commented Feb 25, 2011 at 0:23
  • Also, use memset, calloc or = {0} to initialise, depending on how you're creating the struture. Commented Feb 25, 2011 at 0:26
  • @OrangeDog that is true. But for a noob (maybe rusty is better described) on C you have to admit that I was close... Commented Feb 25, 2011 at 9:06

1 Answer 1

3

You should check that code is less than MAX_NUM.

In your particular case code is probably MAX_NUM + 1

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.