I am trying to iterate over a single const unsigned char
array and assign/convert each element to a new char
array using typecasting, every thread I've read suggests using typecasting however it's not working for me, here's my attempt:
#include <stdio.h>
#include <stdlib.h>
char *create_phone_number(const unsigned char nums[10]) {
char *new = malloc(11);
int i = 0;
for (; i<10; i++)
new[i] = (char)nums[i];
new[i] = '\0';
return new;
}
int main(void) {
char *num = create_phone_number((const unsigned char[]){ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });
printf("%s\n", num);
free(num);
return 0;
}
The above code's stdout:
Expected stdout:
1111111111
How do I convert the elements in nums
to type char
and assign/store the converted values in the new
array (efficiently)?