ok, here is my problem.

I am using sprintf to write a formatted string. That works fine.
it looks like this:

sprintf(buffer, "%f,%f,%i,%f,%f", user.x, user.y, user.dir, user.xspeed, user.yspeed);

where user is a struct
x is a float
y is a float
dir is a short int
xspeed is a float
yspeed is a float

I am parsing the exact same string using sscanf. And I end up with some crazy values.

sscanf(buffer, "%f,%f,%i,%f,%f", &remote.x, &remote.y, &remote.dir, &remote.xspeed, &remote.yspeed);

remote is another member of the same struct as user
x is never right
y is never right
dir is the only one that works i assume it has to do with the fact that it is the only int
xspeed I haven't even tested
yspeed is the same deal as xspeed

Why does this happen? I see no reason for it to not work

Even if I use the correct specifier for a short in sscanf, I see expected results.

#include <stdio.h>

int main(void)
   char buffer[100];
      float x,y;
      short dir;
      float xspeed, yspeed;
   } user = {123.45,6.789,100,43.21,98.76}, remote;
   sprintf(buffer, "%f,%f,%i,%f,%f", user.x, user.y, 
           user.dir, user.xspeed, user.yspeed);
   sscanf(buffer, "%f,%f,%hi,%f,%f", &remote.x, &remote.y, 
          &remote.dir, &remote.xspeed, &remote.yspeed);
   printf("%f,%f,%i,%f,%f", remote.x, remote.y, 
          remote.dir, remote.xspeed, remote.yspeed);
   return 0;

/* my output

Post the code that shows how you have initialized the user struct + buffer and what are the crazy values?

This article has been dead for over six months. Start a new discussion instead.