Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
September 18, 2008 sscanf() compile errors (changed since dmd v0.118)? | ||||
---|---|---|---|---|
| ||||
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 |
September 18, 2008 Re: sscanf() compile errors (changed since dmd v0.118)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AEon | 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 |
September 18, 2008 Re: sscanf() compile errors (changed since dmd v0.118)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars Kyllingstad | 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. |
September 18, 2008 Re: sscanf() compile errors (changed since dmd v0.118)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AEon | 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(). |
September 18, 2008 Re: sscanf() compile errors (changed since dmd v0.118)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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
|
Copyright © 1999-2021 by the D Language Foundation