c - Store int to and retrieve from char array -


what i'm gonna store 2 integers char array, , them array. here's code:

#include <stdio.h>  #define int_width       4 #define buffer_size     10  int main(int argc, char * argv[]) {     char buffer[buffer_size] = {0};     int input1 = 0, input2 = 0;     int output1 = 0, output2 = 0;      printf("int size: %d\n", sizeof(int));      printf("please input 2 integers\n");     scanf("%d", &input1);     scanf("%d", &input2);     printf("the input integers : %d %d\n", input1, input2);      snprintf(buffer, buffer_size, "%d", input1);     snprintf(buffer + int_width, buffer_size - int_width, "%d", input2);      printf("buffer:\n");     (int = 0; < buffer_size; ++i) {         printf("0x%02x ", buffer[i]);     }     printf("\n");      sscanf(buffer, "%d", &output1);     sscanf(buffer + int_width, "%d", &output2);      printf("the output integers retrieved buffer: %d %d\n", output1, output2);      return 0; } 

here's output:

int size: 4 please input 2 integers 100 200 input integers : 100 200 buffer: 0x31 0x30 0x30 0x00 0x32 0x30 0x30 0x00 0x00 0x00 output integers retrieved buffer: 100 200 

it looks fine. when input 1000 , 2000 rather 100 , 200, things go wrong:

int size: 4 please input 2 integers 1000 2000 input integers : 1000 2000 buffer: 0x31 0x30 0x30 0x30 0x32 0x30 0x30 0x30 0x00 0x00 output integers retrieved buffer: 10002000 2000 

where wrong in code?

similarly, tried code below:

#include <stdio.h>  #define buffer_size     10  int main(int argc, char * argv[]) {     char buffer[buffer_size] = {0};     int input1 = 0, input2 = 0;     int output1 = 0, output2 = 0;      printf("please input 2 integers\n");     scanf("%d", &input1);     scanf("%d", &input2);     printf("the input integers : %d %d\n", input1, input2);      snprintf(buffer, buffer_size, "%4d%4d", input1, input2);      printf("buffer:\n");     (int = 0; < buffer_size; ++i) {         printf("0x%02x ", buffer[i]);     }     printf("\n");      sscanf(buffer, "%4d%4d", &output1, &output2);      printf("the output integers retrieved buffer: %d %d\n", output1, output2);      return 0; } 

and here's output:

please input 2 integers 1000 2000 input integers : 1000 2000 buffer: 0x31 0x30 0x30 0x30 0x32 0x30 0x30 0x30 0x00 0x00 output integers retrieved buffer: 1000 2000  please input 2 integers 10000 20000 input integers : 10000 20000 buffer: 0x31 0x30 0x30 0x30 0x30 0x32 0x30 0x30 0x30 0x00 output integers retrieved buffer: 1000 200 

as can see, 1000 , 2000 ok, 10000 , 20000 wrong. wrong?

you not storing integers, storing character strings of digit characters. in lines:

snprintf(buffer, buffer_size, "%d", input1); snprintf(buffer + int_width, buffer_size - int_width, "%d", input2); 

the second snprintf() overwrites nul terminator of first string, , places digits adjacent first. sscanf() requires whitespece delimit fields. in second version, have run out of buffer space. why surprised - trying write 12 characters buffer_size 10?

i wonder why not store integers in int array? if must store numeric strings, why not use 2 dimensional array rather messing around buffer index manipulation.

char buffer[2][int_width] = {{0},{0}} ; ... snprintf( buffer[0], int_width, "%d", input1 ) ; snprintf( buffer[1], int_width, "%d", input2 ) ; ... sscanf( buffer[0], "%d", &output1 ) ; sscanf( buffer[1], "%d", &output1 ) ; 

to store 32bit signed integer of length string need int_width of 12; 10 digits, possible negative sign , nul terminator.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -