2

Conversion of values in structure with _itoa in example below is not successful. Do anybody see any mistake I 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; for (i = 0; i < 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 are correct, in right column not):

0 x 0
20 x k
50 x 1k
50 x 1k
50 x 1k

0 x 0
150 x 50
300 x a0
20 x k
200 x 6k

1 Answer 1

2

The output is correct. You are getting result in base30.

For ex: In base30, 30010 = a030.

Arguments of function _itoa are:

  1. Number to be converted
  2. Destination string
  3. Radix

You are giving your radix as 30, so the output is printed at base 30.

You should call the function as:

_itoa(objects[i].sort, value_string, 10); 

to get the decimal output.

It is quite easy to get confused with 3rd and assume it is size o string for first time users of this function. To avoid such consequences, prefer sprintf

sprintf(value_string, "%d", objects[i].sort); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.