Thanks a lot DaWei. I'm really naive in programming and I did miss the closing brace when posting the code.
I'll try following your suggestions. Btw, the debugger (gdb under Linux) shows that the program stops at i=j=0. So I don't think the bounds were exceeded.
Again, thank you for you time and patience.