Obtaining an address given a (run time) variable name
DLearner
bmqazwsx123 at gmail.com
Sun Jan 26 23:41:53 UTC 2025
On Monday, 20 January 2025 at 19:54:19 UTC, Jonathan M Davis
wrote:
[...]
> Also, there's really no reason to be doing anything with char[]
> instead of string unless you're specifically planning on
> mutating the elements in your string, and casting from a string
> literal to a char[] is almost never a good idea, because it
> means that you could end up mutating the elements, which would
> violate the type system guarantees - since immutable values are
> never supposed to be mutated, and the compiler will potentially
> make the assumption that an immutable value was not mutated
> (string is an alias for immutable(char)[], and string literals
> are strings). If you _do_ need to mutate the string for some
> reason, then use dup to get a mutable copy rather than casting
> the literal, e.g.
>
> char[] wkVarName = "IntVar3".dup;
>
> - Jonathan M Davis
Probably I misunderstand, but using -betterC:
```
extern (C) void main() {
char[] Name;
Name = "InitName".dup;
// Name = cast(char[])("InitName");
}
```
fails to compile, but
```
extern (C) void main() {
char[] Name;
// Name = "InitName".dup;
Name = cast(char[])("InitName");
}
```
compiles.
More information about the Digitalmars-d-learn
mailing list