bizarre print statement glitch
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!
here's the complete code:
Compiled this way, it will show the array's contents successfully transferred to the newarray in Main. But, you un-comment the print statement and recompile, you'll get zeroes for the newarray's contents!!!
Code:
#include <stdio.h>
double * createarray(void);
int main(void)
{
double newarray[3][5] = {0.0};
double * ptr;
int count, total;
ptr = createarray();
// printf("\n\nThis is a TEST print statement!\n\n");
for(total = 5, count = 0; count < total; count++)
newarray[0][count] = *((ptr) + count);
printf("\nTest of Main array content transfer:\n");
for(total = 5, count = 0; count < total; count++)
printf("%.2lf \n", newarray[0][count]);
return 0;
}
double * createarray(void)
{
int count, total;
double array[3][5];
double * createptr = 0;
printf("Each time I prompt you, please enter five floating point values.\n");
printf("Enter *only* your first row of five floating point numbers, please.\n");
for(total = 5, count = 0; count < total; count++)
scanf("%lf", &array[0][count]);
printf("\nTest of original array's contents [first row only]:\n");
for(total = 5, count = 0; count < total; count++)
printf("%.2lf \n", array[0][count]);
createptr = array;
printf("\nTest of original ptr's contents:\n");
for(total = 5, count = 0; count < total; count++)
printf("%.2lf \n", *((createptr) + count) );
return createptr;
}