OT: What causes the Segfault in the following?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 3 18:35:50 PDT 2017


On 08/03/2017 06:02 PM, Andrew Edwards wrote:

 >     char *s;

That's an uninitialized C string.

 >     nk_color_hex_rgb(s, str);

That function is expecting it to have at least 7 chars when doing things 
like

     output[1] = (char)NK_TO_HEX((col.r & 0x0F));

So you have to have a proper pointer to the first element of an array to 
pass to nk_color_hex_rgb. The following may work but you shouldn't be 
needing to use magic constants like 7:
     char[7] s;
     nk_color_hex_rgb(s.ptr, str);
     // ...
     printf("%s\n", s.ptr);

There's probably the proper C macro that defines it so that you can do

   char[BLAH_LENGTH] s:

Ali



More information about the Digitalmars-d-learn mailing list