Okay, this is really weird, but perhaps someone knows why this is happening...
After creating an array in a separate function, and successfully returning the pointer
to main, I've noticed that if I place a simple print statement *before* I use a for statement to assign the values to a new array in main, it won't pass the values. Here are two code snippets, the first is successful, the second fails:
-----------------------------------------------------------------
-----------------------------------------------------------------Code:// this is in main(): { double newarray[3][5] = {0.0}; //newarray created to take createarray's values double * ptr; int count, total; ptr = createarray(); // createarray is separate function that creates array for(total = 5, count = 0; count < total; count++) newarray[0][count] = *((ptr) + count); // print statements after this value assignment indicate success, HOWEVER:
-----------------------------------------------------------------Code:// same code as before: { double newarray[3][5] = {0.0}; double * ptr; int count, total; ptr = createarray(); printf("this is a TEST print statement\n\n"); // this can say anything! for(total = 5, count = 0; count < total; count++) newarray[0][count] = *((ptr) + count); // now, the subsequent print statement to print out the values only reports zeroes!!!
My question is:
Does anyone have any idea why simply placing a print statement *after* the
ptr = createarray();
statement but *before* the
for(total = 5, count = 0; count < total; count++)
newarray[0][count] = *((ptr) + count);
statement somehow prevents the values from being transferred?
I can just remember to always do the value assignments right away, but it seems like there shouldn't be such a restriction.
I'm using Microsoft's Visual C++.
Thanks for any input!



LinkBack URL
About LinkBacks



