c - Conversion with _itoa -
conversion of values in structure _itoa in example below not successful. see mistake made?
/* typedef struct struct_rect { int x; int y; int w; int h; }rectangle; struct struct_objects { int sort; rectangle rect; allegro_color color; }objects[1000]; int number_of_objects = 2; */ bool print_objects() { char value_string[100]; int i; (i = 0; < number_of_objects; i++) { memset(value_string, '\0', sizeof(value_string)); _itoa(objects[i].sort, value_string, 30); printf("%i x %s\n", objects[i].sort, value_string); memset(value_string, '\0', sizeof(value_string)); _itoa(objects[i].rect.x, value_string, 30); printf("%i x %s\n", objects[i].rect.x, value_string); memset(value_string, '\0', sizeof(value_string)); _itoa(objects[i].rect.y, value_string, 30); printf("%i x %s\n", objects[i].rect.y, value_string); memset(value_string, '\0', sizeof(value_string)); _itoa(objects[i].rect.w, value_string, 30); printf("%i x %s\n", objects[i].rect.w, value_string); memset(value_string, '\0', sizeof(value_string)); _itoa(objects[i].rect.h, value_string, 30); printf("%i x %s\n", objects[i].rect.h, value_string); } return true; }
output is(values in left column correct, in right column not):
0 x 0
20 x k
50 x 1k
50 x 1k
50 x 1k0 x 0
150 x 50
300 x a0
20 x k
200 x 6k
the output correct. getting result in base30.
for ex: in base30, 30010 = a030.
arguments of function _itoa
are:
- number converted
- destination string
- radix
you giving radix 30, output printed @ base 30.
you should call function as:
_itoa(objects[i].sort, value_string, 10);
to decimal output.
it quite easy confused 3rd , assume size o string first time users of function. avoid such consequences, prefer sprintf
sprintf(value_string, "%d", objects[i].sort);
Comments
Post a Comment