Q:

Concatenating uint8_t to a char*

im really new to C and im having a bit of a complication creating a char* from various uint8_t
My idea is to create a char* where in each location I place a number form a matrix
For example if I have a matrix with:
[1][2][3][4]
[5][6][7][8]
[9][0][1][2]
[3][4][5][6]

id like a char* thats "01234567890123456"
what im doing bit its not working is:
char* string = malloc(sizeof(char)*matrix->height*matrix->width);

for (int i = 0; i < matrix->height ; ++i) {
        for (int j = 0; j < matrix->width ; ++j) {
                string[i*matrix->height+j] = matrix->value[i][j];
            }
}

of course its not working but im a bit lost on how to proceed and I cant find more information regarding this problem.
Any help would be nice,
thanks

A:

Since you're trying to print a string, you need the ASCII character for 0. So, simply add '0' to each number, like so
char* string = malloc(sizeof(char)*(matrix->height*matrix->width + 1));

for (int i = 0; i < matrix->height ; ++i) {
    for (int j = 0; j < matrix->width ; ++j) {
        string[i*matrix->width+j] = matrix->value[i][j] + '0';
    }
}

string[matrix->height*matrix->width] = 0; //null terminator

Note however this isn't exactly the most portable solution. 
Also, notice that you want to multiply i by the width, because if you didn't have a square matrix your calculation wouldn't work correctly.
It's kind of unnecessary to have sizeof(char), because the size of a char is defined to be 1 regardless of the byte size. 

