c - Concatenate char array and int array and store in string array -
i concatenate char array , int array , store them in char array. how that?
here code far
char letter[100]; int number[100], i; char * letternum[100]; (i = 0; < 100; i++){ letternum[i] = strcat(letter[i], number[i]); } expected output should
a1
b1
...
the strcat() function concatenates strings not chars, need sprintf()
sprintf(letternum[i], "%c%d", letter[i], number[i]); and also, letternum in case array of pointers, should array of arrays, like
char letternum[100][3]; and then, can use snprintf() instead of sprintf() prevent buffer overflow
if (snprintf(letternum[i], 3, "%c%d", letter[i], number[i]) > 2) youhavetodosomethin_an_error_occurred();
Comments
Post a Comment