Strange issue with printing array value
The problem below is related to my other recent forum post,
http://cboard.cprogramming.com/c-pro...tml#post981978
I've learned more about what's going on, and decided to start a new thread to succinctly summarize this ongoing strange problem:
In my code, within one of my functions (functionA()), I call another function (functionB( )), which calculates and assigns the values in MyArray[ ], which is an array of doubles that is created and intialized to 0.0 within functionA(). I've verified that the calculated values assigned to MyArray[ ] within functionB( ) are always correct at the very end of functionB().
Immediately upon return from functionB(), back in functionA(), when I print out the values in MyArray(), strange things are happening.
Initially, I had simply had the print statement:
Code:
printf("Back from functionB(): MyArray[0] = %f, MyArray[1] = %f, MyArray[2] = %f, ...\n", MyArray[0],MyArray[1],MyArray[2],...);
Every time, the value of MyArray[0] always showed NAN instead of a real value (but all other values in MyArray were fine), which led me to think that there was some problem with the actual value of MyArray[0].
But now that I've added some additional print statements, I've found something odd: When I inserted the following print statements *around* my original one above:
Code:
printf("Back from funcB() #1: MyArray[0] = %f.\n", MyArray[0]);
printf("Back from funcB() #2: MyArray[0] = %f.\n", MyArray[0]);
printf("Back from functionB(): MyArray[0] = %f, MyArray[1] = %f, MyArray[2] = %f, ...\n", MyArray[0],MyArray[1],MyArray[2],...);
printf("Back from funcB() #3: MyArray[0] = %f.\n", MyArray[0]);
printf("Back from funcB() #4: MyArray[0] = %f.\n", MyArray[0]);
...*all* of the new MyArray[0] values printed out, before and after my original statement, are all fine! And yet the longer print statement above (in the middle of the new additions) continues to show MyArray[0] = NAN.
If I substitute MyArray[1] or some other value in for MyArray[0] in the long print statement above, *any* of these other values *also* prints out as NAN! So apparently, there is a problem with whichever first value is being printed in this long print statement, and not specifically a problem with the value of MyArray[0].
Also, I made a copy of the long print statement above, and pasted it on the next line after my original long print statement. Every value in this 2nd copy is fine, including MyArray[0]. However, if I comment out the original long statement and only allow the copy of this long statement to remain, the first value printed out is now NAN.
I've checked and checked again for a syntax error in my long printf( ) statement, but I haven't found anything...
My code system is fairly complex, so I don't think that including extensive code in this thread is going to be useful or necessary to solve this problem. This *seems* to be a weird but isolated issue... any suggestions about what might possibly be going wrong, such that the value of MyArray[0] could be printed fine before my long print statement above, but then always displays erroneously later on, even though no changes to the values in MyArray[ ] are being made?
Thanks in advance for your input.