On Monday, 20 January 2025 at 19:54:19 UTC, Jonathan M Davis wrote:
On Monday, January 20, 2025 12:03:09 PM MST DLearner via Digitalmars-d-learn wrote:
// In the code below, what fn needs to go in the commented-out
line,
// to make the program print 7, 11 rather than 7, 7?

```
void main() {

    import std.stdio;

    int IntVar1 = 5;
    int IntVar2 = 7;
    int IntVar3 = 11;

    int*   wkVarAddr;
    char[] wkVarName;


    wkVarAddr = &IntVar2;
    writeln("*wkVarAddr = ", *wkVarAddr);

    wkVarName = cast(char[])("IntVar3");
//   wkVarAddr = fn (wkVarName);
    writeln("*wkVarAddr = ", *wkVarAddr);
}
```


Variable names are not a thing at runtime.
[...]
But the language isn't going to do anything like that for you automatically.

Agreed, I need to think about this further.


Also, there's really no reason to be doing anything with char[] instead of string
[...]
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;

The reason for using char[] wasn't because there was a need to mutate the elements. It was because my understanding was that in certain situations the string construct did not just produce a character array - it also produced the (to me horrible) concept of adding a x'00' immediately after the last character of the array. Which could produce problems on concatenation, and doubt about the length of the string.
char[] just worked properly.





- Jonathan M Davis


Reply via email to