const pointers C vs. D

Dennis dkorpel at gmail.com
Tue Feb 4 10:17:39 UTC 2020


On Tuesday, 4 February 2020 at 10:06:03 UTC, Johann Lermer wrote:
> In C, this would not be valid. So the question for me now is: 
> is const char* in D different from C?

Yes, const char* in D reads as const(char*), so it is a char* 
that cannot be modified.
This is similar to the C code:

char *const text = "Hello";

However, because of transitivity, the characters also can't be 
modified (unlike C).
For a mutable pointer to const characters, you indeed do 
const(char)*.

See also:
https://dlang.org/articles/const-faq.html

>C++ has a const system that is closer to D's than any other 
>language, but it still has huge differences:
>
>- const is not transitive
>- no immutables
>- const objects can have mutable members
>- const can be legally cast away and the data modified
>- const T and T are not always distinct types




More information about the Digitalmars-d-learn mailing list