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

Lars Kyllingstad public at kyllingen.NOSPAMnet
Thu Sep 18 05:42:40 PDT 2008


AEon 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?

I don't know whether such casting is considered bad practice. Anyway, 
you should use the 'ptr' property of arrays instead, as it's cleaner.

     int ret = sscanf( line.ptr, ttempl.ptr, &min, &sec );

Check out http://www.digitalmars.com/d/1.0/arrays.html and look for 
"Dynamic Array Properties". You'll find it there.

> 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*,...); )).

Sorry, can't help you there.

-Lars


More information about the Digitalmars-d-learn mailing list