2

I'm having a problem with the following piece of code throwing warnings and hoping you could help me:

   fprintf (fp, "%dd%d+%d ", pMobIndex->mana[DICE_NUMBER],

DICE_NUMBER is defined in my header file as 0.

Obviously, 0 is not exceeding the size of the array.

The array is defined as.

   int               mana[2];

I have absolutely no idea why it would be doing this, as 0 is clearly within the bounds of the array. Half of my engines code is throwing these array bound errors now, I've got about 30 of them, and NONE of them make sense to me.

Here is the output from make:

  gcc -O3 -s -Wall -c -o obj/olc_save.o olc_save.c
olc_save.c: In function 'save_mobile':
olc_save.c:234:13: warning: array subscript is above array bounds [-Warray-bounds]
     fprintf (fp, "%dd%d+%d ", pMobIndex->mana[DICE_NUMBER],
         ^

it also happens:

db1.c: In function 'create_mobile':
db1.c:2056:30: warning: array subscript is above array bounds [-Warray-bounds]
             + pMobIndex->mana[DICE_BONUS];

and

olc_act.c: In function 'medit_manadice':
olc_act.c:6500:15: warning: array subscript is above array bounds [-Warray-bounds]
     pMob->mana[DICE_BONUS] = atoi (bonus);

The definition in my header file:

/* dice */
#define DICE_NUMBER  0
#define DICE_TYPE    1
#define DICE_BONUS   2

I'm aware DICE_BONUS will be (realizing it only now) but I for the life of me cannot figure out why DICE_NUMBER is.

D'oh. The problem is that the third integer output there on the fprintf is DICE_BONUS but its on another line, I thought the compiler was warning me about DICE_NUMBER, it was warning me about BONUS.

4
  • 6
    SSCCE please, or it didn't happen. Commented Sep 28, 2013 at 10:48
  • Are you sure that Warning is because of code shown??? Commented Sep 28, 2013 at 10:49
  • Update to your own question plz, not as a comment. Commented Sep 28, 2013 at 10:53
  • 2
    DICE_BONUS will obviously be out of bounds in your case Commented Sep 28, 2013 at 10:58

1 Answer 1

3

mana[2] is an integer array for two elements and DICE_BONUS is defined as 2 so mana[2] means you are trying to access third element.

REMEMBER array starts from 0 subscript. So all warnings related to DICE_BONUS is valid. You need to redefine your array for three elements.

Now about DICE_NUMBER that is not causing warning. Perhaps you have some additional argument on that line which includes DICE_BONUS there too.

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

1 Comment

In fact, considering that his format string takes three integers meant to display stuff like 4d6 + 1 it's seems likely that the rest of the printf function call includes pMobIndex->mana[DICE_BONUS] further on.

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.