1

I call printf function consecutively and first printf writes what it must write, second printf just writes invalid values and I guess some errors.

Type of "LOC" is defined by me and variable "i" doesn't change after callings. I checked double for any discrepancy between printf and values I give it.

i=2;
printf("x = %f,y = %f,z = %f\n",(*LOC)[0].ProjectionPoints[i].X,(*LOC)[0].ProjectionPoints[i].Y,(*LOC)[0].ProjectionPoints[i].Z); /* Prints perfectly */
printf("x = %f,y = %f,z = %f\n",(*LOC)[0].ProjectionPoints[i].X,(*LOC)[0].ProjectionPoints[i].Y,(*LOC)[0].ProjectionPoints[i].Z); /* Shows some errors and values are "0" */

First printf writes

x = -10.000000,y = -8.000000,z = -10.000000

Second printf writes

x = 0.000000,y = 0.000000,z = -1.#QNAN0

Even defining variables as const isn't change anything.

My compiler is: MS Visual C++ 2012

After I read comments;

typedef struct {
P3C_Point *ProjectionPoints;
uint Distance,LayerID,NumberOfPoints,ModelID;} P3C_LayerOnCurtain;
/* and */
P3C_LayerOnCurtain **LOC = P3C_Compile(Stream);
/* when I try printf in P3C_Compile it has no problems */

I made test before I return value, in function produces "LOC" is printf perfectly.

Answer/Solution;

 P3C_LayerOnCurtain *LOC = *P3C_Compile(Stream);
user1156885
  • 51
  • 10

1 Answers1

5

The most probable cause would be that LOC or (*LOC[0].ProjectionPoints) are pointers to a local variable of a function that returned them.

If so, they would be overwritten by printf's local variables.

Medinoc
  • 6,577
  • 20
  • 42
  • are all local variables write in same adress? – user1156885 May 31 '13 at 09:12
  • Local variables are, for all intents and purposes, destroyed when the function returns. Any subsequent function call will overwrite that space with its own local variables. "same address" is a bit of a misnomer, because local variables can have different sizes. – Medinoc May 31 '13 at 09:16
  • On a classic x86 architecture, local variables are stored on the stack: They are "pushed" when created and "popped" when destroyed. Function calls also use the stack to store the return address, so functions with no local variables and lots of nested calls would overwrite local variables as well. – Medinoc May 31 '13 at 09:19