I was completely baffled withis problem. I have rather large C program. While debugging it I got to a point where the index of a "for" seems not to behave correctly. As the program is lage, I extracted from it the part originating the problem, as follows:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define HASHSIZE 8
enum {NOTAB, RINGTAB, MODTAB};
typedef struct
{
char u;
char v;
} hashing;
typedef struct // For any parameter input
{
char *key;
char *anydata;
int attrib;
int use;
double prob;
int *place;
} general;
int X;
hashing Htable[HASHSIZE];
general Tabring[] =
{
{"", 0, '+', 0, 0, &X},
{"AND", (char *)&Tabring, '=', 0, 0, &X},
{"EQ", (char *)&Tabring, '=', 0, 0, &X},
{"NAND", (char *)&Tabring, '=', 0, 0, &X},
{"", "*", '*', 0, 0, &X}
};
general Tabmod[] = // Table of modifier names
{
{"", 0, '&', 0, 0, &X},
{"PHASE", (char *)&Tabmod, '=', 0, 0, &X},
{"RAND", (char *)&Tabmod, '=', 0, 0, &X},
{"", "*", -1, 0, 0, &X}
};
general *Tab[] = {NULL, Tabring, Tabmod, NULL};
void clear(void)
{
int i;
for (i = 0; i < HASHSIZE; i++)
Htable.v = Htable.u = 0;
}
void fillhash(char n, int k, char *key)
{
int j, i = 0, x = n, y = 0;
j = (int)key[0];
while (j > 32)
{
y += (x += j);
j = (int)key[++i];
}
y &= HASHSIZE - 1;
while (Htable[y].u)
{
y++;
y &= HASHSIZE - 1;
}
Htable[y].u = n;
Htable[y].v = (char)k;
}
void inittables(void)
{
int i;
for (i = 1; Tabring.key[0]; i++)
fillhash(RINGTAB, i, Tabring.key); // <- Here
for (i = 1; Tabmod.key[0]; i++)
fillhash(MODTAB, i, Tabmod.key);
}
int main(int argc, char *argv[])
{
int i;
clear();
inittables();
for (i = 0; i < HASHSIZE; i++)
printf("Hashtable[%d] = %d %d\n", i, Htable.u, Htable.v);
return 0;
}
This is a complete program, and runs perfectly. But in the large program, when debugging it, in the place signalled by "Here", index i becomes 2 instead of 1 the first time, and then it continues 3, 4...
Please, does somebody have an explanation?