| Thread overview | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 20, 2004 What am I missing? | ||||
|---|---|---|---|---|
| ||||
This compiles but, I get 'opCmp: Error: Access Violation'
Any thoughts?
class fred {
char[] opAdd(fred a){ return "Hello"; }
}
int main(char[][] args)
{
fred f = new fred();
fred g = new fred();
char[] s;
s = g + f;
printf("opCmp: %s", s ~ "\0" );
return 0;
}
| ||||
April 20, 2004 Re: What am I missing? (Warning %s problem yet again) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Scott Egan | Scott Egan wrote: >This compiles but, I get 'opCmp: Error: Access Violation' > >Any thoughts? > >class fred { > char[] opAdd(fred a){ return "Hello"; } >} > >int main(char[][] args) >{ > fred f = new fred(); > fred g = new fred(); > char[] s; > > s = g + f; > > printf("opCmp: %s", s ~ "\0" ); > > printf("opCmp: %.*s", s ~ "\0" ); If of D arrays as a struct with a length at the front. > return 0; >} > > > > -- -Anderson: http://badmama.com.au/~anderson/ | |||
April 20, 2004 Re: What am I missing? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Scott Egan | In article <c631su$20rf$1@digitaldaemon.com>, Scott Egan says... > >This compiles but, I get 'opCmp: Error: Access Violation' > >Any thoughts? > >class fred { > char[] opAdd(fred a){ return "Hello"; } >} > >int main(char[][] args) >{ > fred f = new fred(); > fred g = new fred(); > char[] s; > > s = g + f; > > printf("opCmp: %s", s ~ "\0" ); Try %.*s > return 0; >} > > | |||
April 20, 2004 Re: What am I missing? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Scott Egan |
printf("opCmp: %s", s ~ "\0" );
This should either be (as other have said)
printf("opCmp: %.*s", s ~ "\0" );
or
printf("opCmp: %s", (char*)(s ~ "\0") );
| |||
April 20, 2004 Re: What am I missing? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Ben Hinkle wrote: > printf("opCmp: %s", s ~ "\0" ); > >This should either be (as other have said) > printf("opCmp: %.*s", s ~ "\0" ); >or > printf("opCmp: %s", (char*)(s ~ "\0") ); > > It would be cool if we could have a bot that automaticly directed these questions to the wiki <g> ie it would search for two words, printf and %s, 97% of the time I reckon it would hit correct (sorry for sending a personal email :( ) -- -Anderson: http://badmama.com.au/~anderson/ | |||
April 20, 2004 Re: What am I missing? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Ben Hinkle wrote: > > printf("opCmp: %s", s ~ "\0" ); > > This should either be (as other have said) > printf("opCmp: %.*s", s ~ "\0" ); Which makes the null terminator redundant. > printf("opCmp: %s", (char*)(s ~ "\0") ); Or printf("opCmp: %s", toStringz(s) ); Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. | |||
April 20, 2004 Re: What am I missing? (Warning %s problem yet again) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to J Anderson | Sorry, I'll try not to be so stupid in the future (but I can't promise anything). "J Anderson" <REMOVEanderson@badmama.com.au> wrote in message news:c63314$22ci$1@digitaldaemon.com... > Scott Egan wrote: > > >This compiles but, I get 'opCmp: Error: Access Violation' > > > >Any thoughts? > > > >class fred { > > char[] opAdd(fred a){ return "Hello"; } > >} > > > >int main(char[][] args) > >{ > > fred f = new fred(); > > fred g = new fred(); > > char[] s; > > > > s = g + f; > > > > printf("opCmp: %s", s ~ "\0" ); > > > > > printf("opCmp: %.*s", s ~ "\0" ); > > If of D arrays as a struct with a length at the front. > > > return 0; > >} > > > > > > > > > > > -- > -Anderson: http://badmama.com.au/~anderson/ | |||
April 20, 2004 Re: What am I missing? (Warning %s problem yet again) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to J Anderson | J Anderson schrieb: > Scott Egan wrote: > >> This compiles but, I get 'opCmp: Error: Access Violation' >> >> Any thoughts? >> >> class fred { >> char[] opAdd(fred a){ return "Hello"; } >> } Always returns a constant array literal. >> int main(char[][] args) >> { >> fred f = new fred(); >> fred g = new fred(); >> char[] s; >> >> s = g + f; From here s points to constant array literal. >> printf("opCmp: %s", s ~ "\0" ); >> >> > printf("opCmp: %.*s", s ~ "\0" ); > > If of D arrays as a struct with a length at the front. doesn't matter here, since pointer is the first in the function parameter parsing queue. Maybe the error happens at concatenation - but why? I think only assembly level debugging would help here. Besides, try splitting away concatenation onto a separate line. Make a new variable, don't use =~ else you might loose the bug. Sorry that i can't be more helpful at the moment. -eye | |||
April 20, 2004 Re: What am I missing? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon |
>> printf("opCmp: %s", (char*)(s ~ "\0") );
>
>Or
>
> printf("opCmp: %s", toStringz(s) );
oh yeah - it had been so long since I last toStringz'ed something I
had forgotten all about it. I'll take that as a good sign :-)
-Ben
| |||
April 21, 2004 Re: What am I missing? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to J Anderson | J Anderson wrote: > Ben Hinkle wrote: > >> printf("opCmp: %s", s ~ "\0" ); >> >> This should either be (as other have said) >> printf("opCmp: %.*s", s ~ "\0" ); >> or >> printf("opCmp: %s", (char*)(s ~ "\0") ); >> >> > It would be cool if we could have a bot that automaticly directed these questions to the wiki <g> > > ie it would search for two words, printf and %s, 97% of the time I reckon it would hit correct > > (sorry for sending a personal email :( ) In the meantime, we could take turns manually directing them to: http://www.wikiservice.at/d/wiki.cgi?FaqRoadmap#RuntimeErrors or http://www.wikiservice.at/d/wiki.cgi?ErrorMessages -- Justin http://jcc_7.tripod.com/d/ | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply