3

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

1 Answers1

5

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.

user975989
  • 2,578
  • 1
  • 20
  • 38
  • It's also probably worth pointing out that the result is not a C string as the OP probably intends. Due to no NUL terminator. And what is the reason for changing to `sizeof(uint8_t)`? Though the result is likely to be the same isn't it better to use the same type as the array elements? – kaylum Jun 07 '16 at 23:28
  • You're right, thanks. Also, I didn't change the `sizeof`, it was like that when I originally answered, the question got edited. – user975989 Jun 07 '16 at 23:30
  • yeah sorry I edited the question, made more sense to have size of char. Im getting mixed results with my code (the string is not complete when I add an extra condition to my code (row is enabled) ) Im getting the number is: `7 the number is: 6 the number is: 7 the number is: 6 string: 7 ` where string should be 7676 – Jose Maria de la Torre Jun 07 '16 at 23:34
  • Row is enabled? Do you mean you only print some rows? If so, you are going to get segments of the string into which you haven't written, and since you used `malloc` it's filled with garbage. Use a counter to keep track of how many elements you skip, so that you have the proper offset into the string. Don't forget about writing the null terminator too. – user975989 Jun 07 '16 at 23:37
  • yep thats exactly it. Thanks a lot for your answer :) – Jose Maria de la Torre Jun 07 '16 at 23:38
  • If you want to keep `sizeof(char)` then the `+1` should be multipled into that, not added on afterwards (null terminator size is also 1 char) – M.M Jun 08 '16 at 02:38