char* pointers between C and D

H. S. Teoh hsteoh at qfbox.info
Mon Jul 25 17:41:26 UTC 2022


On Mon, Jul 25, 2022 at 05:30:14PM +0000, pascal111 via Digitalmars-d-learn wrote:
[...]
> int main(string[] args)
> {
> 
> 
>         const(char)[] ch1 = "Hello World!";
>         char[] ch2="Hello World!".dup;
> 
>         const(char) *p1;
>         char *p2;
> 
>         ch2~="\0";
> 
>         p1=ch1.ptr;
>         p2=ch2.ptr;
> 
>         writeln(p1[0..strlen(p1)]);
>         writeln(p2[0..strlen(p2)]);

Unless you are passing the string back to C code as char*, there is no
need to use strlen here. You can just use `writeln(p1);` to output the
entire string, or slice it with `p1[0 .. $]` to get the entire string.


T

-- 
Being able to learn is a great learning; being able to unlearn is a greater learning.


More information about the Digitalmars-d-learn mailing list