cpvstr

Discuss any Chipmunk bugs here.
Post Reply
User avatar
juanpi
Posts: 24
Joined: Wed Jan 30, 2008 3:52 pm
Contact:

cpvstr

Post by juanpi »

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!
supertommy
Posts: 56
Joined: Tue Sep 11, 2007 2:30 pm
Contact:

Re: cpvstr

Post by supertommy »

Look at cpvstr:

Code: Select all

char*
cpvstr(const cpVect v)
{
    static char str[256];
    sprintf(str, "(% .3f, % .3f)", v.x, v.y);
    return str;
}
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.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: cpvstr

Post by slembcke »

From the documentation:
NOTE: The string points to a static local and is reset every time the function is called.
Sorry. If you need something fancier, you'll have to split up your printf() calls or stringify them yourself.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
juanpi
Posts: 24
Joined: Wed Jan 30, 2008 3:52 pm
Contact:

Re: cpvstr

Post by juanpi »

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
:D
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests