Can I output strings using core.stdc.stdio?
Ferhat Kurtulmuş
aferust at gmail.com
Wed Dec 23 09:40:27 UTC 2020
On Wednesday, 23 December 2020 at 09:06:02 UTC, Godnyx wrote:
> On Wednesday, 23 December 2020 at 08:50:50 UTC, Mike Parker
> wrote:
>> On Wednesday, 23 December 2020 at 08:45:15 UTC, Godnyx wrote:
>>
>>> Yep and I find it out! It won't work with templates and/or
>>> variadic function parameters. It says that the variable can't
>>> be read at compile time (so I can't cast it) or it will work
>>> but it will give me a segmentation fault (lol hello C). Any
>>> idea why this is happening in those cases?
>>
>> Please show the code that's causing the error. Without it, all
>> anyone can do is keep making suggestions that *might* be the
>> problem. With the code, someone can point to it exactly.
>
> Yep that's the best thing I can do! Code:
>
> import core.stdc.stdio : printf;
> import std.string : toStringz;
>
> void put(A...)(string prompt, A args) {
> for (ulong i = 0; i < args.length; i++) {
> if (typeof(args[i]).stringof == "string")
> printf("%s\n", args[i].toStringz);
> }
> }
>
> void main() {
> string h = "World!";
> string w = "World!";
> put(h, w);
> }
>
> I'm getting two errors. First that i can't be read at compile
> time and second that I don't initialize the function right. So
> I know I'm doing something wrong but I don't know why... Any
> ideas?
I didn't dive into your use case, but you should use static
foreach in this case:
void put(A...)(string prompt, A args) {
static foreach (ulong i; 0..args.length) {
if (typeof(args[i]).stringof == "string")
printf("%s\n", args[i].toStringz);
}
}
More information about the Digitalmars-d-learn
mailing list