Can we use strings with scanf?

Mike Parker aldacron at gmail.com
Mon Jan 25 10:33:14 UTC 2021


On Monday, 25 January 2021 at 09:16:11 UTC, Rempas wrote:
> For printf() we can use this format `"%.*s", cast(int)s 
> s.length, s.ptr`. When trying to use the same for scanf(), it 
> says that this specifier is invalid.

The * has a different meaning for scanf than for printf ([1] vs 
[2]).

There's also the issue that a string is immutable(char)[].

If you really, really, really, must use scanf:

```
char[bufSize] buf;
scanf("%s", buf.ptr);
```

But please don't. This is D, not 1990s C.

[1] https://www.cplusplus.com/reference/cstdio/scanf/
[2] https://www.cplusplus.com/reference/cstdio/printf/


More information about the Digitalmars-d-learn mailing list