sscanf() compile errors (changed since dmd v0.118)?

Jarrett Billingsley jarrett.billingsley at gmail.com
Thu Sep 18 06:33:19 PDT 2008


On Thu, Sep 18, 2008 at 8:26 AM, AEon <aeon2001 at lycos.de> wrote:
> Continuing to compile my 3.5 year old D code, the compiler yields this
> error:
>
>
> dmd -c -w -version=db_log -I. aepar_p_q3a.d
> aepar_p_q3a.d(189): function std.c.stdio.sscanf (char*,char*,...)
>                    does not match parameter types (char[],char[],int*,int*)
> aepar_p_q3a.d(189): Error: cannot implicitly convert expression (line)
>                    of type char[] to char*
> aepar_p_q3a.d(189): Error: cannot implicitly convert expression (ttempl)
>                    of type char[] to char*
>
>
> aepar_p_q3a.d:
> ---snip---
> int rip_Time_in_Seconds( char[] line )
> {
>    char[] ttempl = "%d:%d";    // Time Template
>    int min, sec;
>
>    int ret = sscanf( line, ttempl, &min, &sec );       // Line 189
>    int uptime = min * 60 + sec;
>
>    return uptime;
> }
> ---snip---
>
>
> I want to keep using "char[] line" from my main function, so
> what would be the clean way to avoid the compiler error?
>
> Would casting be "good"? :
>
>    int ret = sscanf( cast(char*)line, cast(char*)ttempl, &min, &sec );
>
> Works, no compiler error, but am I hacking something I should not?

Implicit conversion from T[] to T* was removed for exactly this kind
of case -- accidentally passing a D-style string to a function that
expects a C-style string.  ttempl.ptr should work since the D compiler
implicitly zero-terminates string literals, but line.ptr will probably
not work since line could come from anywhere and may or may not have a
terminating NUL character.  The proper way to do it is to
toStringz(line).

>
> Are there any "new" Phobos native commands that should
> rather be used than the probably outdated sscanf() (from
> std.c.stdio: sscanf() int sscanf(char*, char*,...); )).

import std.cstream, use din.readf().


More information about the Digitalmars-d-learn mailing list