Hi,
The line
printf("%s %s\n", cpvstr(body1->p),cpvstr(body2->p))
gives a different result than
printf("%s ", cpvstr(body1->p));printf("%s\n",cpvstr(body2->p))
Where the second call gives the correct output.
I looked at the function but I can not see why is this?
Thanks!
cpvstr
-
- Posts: 56
- Joined: Tue Sep 11, 2007 2:30 pm
- Contact:
Re: cpvstr
Look at cpvstr:
It returns a pointer to a function-static character array. This means that the call to cpvstr(body2->p) will overwrite the string generated in cpvstr(body1->p). Doing this in two printfs works because you will print the first string before overwriting it with the second.
Code: Select all
char*
cpvstr(const cpVect v)
{
static char str[256];
sprintf(str, "(% .3f, % .3f)", v.x, v.y);
return str;
}
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: cpvstr
From the documentation:
Sorry. If you need something fancier, you'll have to split up your printf() calls or stringify them yourself.NOTE: The string points to a static local and is reset every time the function is called.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
- juanpi
- Posts: 24
- Joined: Wed Jan 30, 2008 3:52 pm
- Contact:
Re: cpvstr
Thanks!
I had read the note in the documentation but was not sure of what it meant.
I guess is because an incomplete knowledge of how multiple arguments to printf are treated. Heuristic approach to programming, jejeje
I had read the note in the documentation but was not sure of what it meant.
I guess is because an incomplete knowledge of how multiple arguments to printf are treated. Heuristic approach to programming, jejeje
Who is online
Users browsing this forum: No registered users and 4 guests