0

The following code Compiled but gave me an error (credit.c:21:7: runtime error: index 15 out of bounds for type 'int [15]') on the last line included while running:

int m;
int r;
int counter;

int main (void)
{
printf ("Card #:");

long long card = get_long_long();
int multiplied[15];
int reg[15];
counter = 1;
m = 0;
r = 0;
do {
  if (counter % 2 == 0){
  r++;
  reg[r] = card % 10;

If anyone knows what this means please help. I initially waned to initiate array at 1 and add as needed later via a variable in the brackets but it wouldn't let me.

1 Answer 1

1

reg is declared as an array with 15 elements. It has indexes 0 through 14. There is no reg[15]. Without seeing the rest of the code, one might assume it fails after r++ executes for the 15th time.

4
  • I actually use it less than 15 times and skip zero. Is that a problem? I'd like to be more precise, but the size would depend on user input. Commented Jul 16, 2017 at 2:26
  • If you are getting this message credit.c:21:7: runtime error: index 15 out of bounds for type 'int [15]') AND I am understanding your question correctly then r is 15 here reg[r] = card % 10;. Maybe it's time for debug50? Commented Jul 16, 2017 at 3:20
  • Actually, I set r to 0 and the program changed it to one Commented Jul 16, 2017 at 4:22
  • it seems to be something wrong with a later piece of code. A loop that won't end. Thanks for the tip to use debug50! Commented Jul 16, 2017 at 4:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .