OT: What causes the Segfault in the following?

Andrew Edwards via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 3 19:37:02 PDT 2017


Ali Çehreli wrote:
> On 08/03/2017 06:02 PM, Andrew Edwards wrote:
>
>>     char *s;
>
> That's an uninitialized C string.

OK, I was is indeed the problem. I was thinking for some reason that s 
gets initialized inside nk_color_hex_rgb() but it's expecting to an 
array to work with. I actually noticed that couldn't, for the life of 
me, associate it to the cause of the resulting issue.

>
>>     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