Search code examples
carraysstringcharint

Storing multiple integers in a char array


I've been looking online for a few hours to find this, but haven't found anything yet that's exactly what I need. I have multiple integers that I need to put into a char* (separated by a space) to be later written to a .txt file. My best attempt until now has been this:

char* temp = (char)input.Z_pos_Camera_Temperature;

Where input.Z_pos_Camera_Temperature is a member of a struct. I tried

char* temp = (char)input.Z_pos_Camera_Temperature + ' ' + input.Z_neg_Camera_Temperature;

but that only added the values of the three chars individually. Can someone help me figure this out?


Solution

  • You may want to use snprintf.

    char buf[32]; snprintf(buf, 32, "%d %d", input.Z_pos_Camera_Temperature, input.Z_neg_Camera_Temperature);