www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - sscanf() compile errors (changed since dmd v0.118)?

reply AEon <aeon2001 lycos.de> writes:
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?


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

Thanks

AEon
Sep 18 2008
next sibling parent reply Lars Kyllingstad <public kyllingen.NOSPAMnet> writes:
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
Sep 18 2008
parent AEon <aeon2001 lycos.de> writes:
Lars Kyllingstad wrote:

 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 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.

Ah... yes that looks a lot less like a "hack". Works fine.
Sep 18 2008
prev sibling parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Thu, Sep 18, 2008 at 8:26 AM, AEon <aeon2001 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().
Sep 18 2008
parent AEon <aeon2001 lycos.de> writes:
Jarrett Billingsley wrote:
 On Thu, Sep 18, 2008 at 8:26 AM, AEon <aeon2001 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).

I had forgotten all about toStringz(). Using it now, code seems to work. AEon
Sep 18 2008