Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
June 08, 2003 &buf[0] problem | ||||
---|---|---|---|---|
| ||||
When you write an innocent char buf[256]; .... printf("buf=%s\n",buf); You get an buf=Error: Access Violation You need to write printf("buf=%s\n",&buf[0]); to get the code to work correctly. Of course it's the same with fgets(&buf[0],buf.size,c.stdio.stdin); and dozens of other C functions that take char * (in fact array) parameters. This will cost a lot of progammers that are used to C a lot of time to get used to. IMHO D should either - give a clear error message (and not silently take the char [] without converting it correctly ) or - support the normal array passing semantics for functions that are "extern C". -- Helmut Leitner leitner@hls.via.at Graz, Austria www.hls-software.com |
June 08, 2003 Re: &buf[0] problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Helmut Leitner | In article <3EE2FC42.63A0F747@chello.at>, Helmut Leitner says...
>
>When you write an innocent
>
> char buf[256];
> ....
> printf("buf=%s\n",buf);
>
>You get an
>
> buf=Error: Access Violation
>
>You need to write
>
> printf("buf=%s\n",&buf[0]);
>
>to get the code to work correctly.
>
>Of course it's the same with
> fgets(&buf[0],buf.size,c.stdio.stdin);
>and dozens of other C functions that take char * (in fact array)
>parameters.
>
>This will cost a lot of progammers that are used to C
>a lot of time to get used to.
To call C functions from Ada you have to do the same thing, explicitly pass the address of buf[0], which is not a bad thing since that clearly states what is going on. The C idiom of array name is equivalent to address of first element should probably not be carried forward into D since it will probably prevent good type checking. I do agree this should be strongly docummented in any language manuals. Remember D is not supposed to be a syntax and semantically compatible with C just easily interfaced. When you use a new language you have to get used to it's syntax and semantics. A few extra characters for clarity will not hurt anyone. Welcome to strongly typed languages.
|
June 08, 2003 Re: &buf[0] problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark T | Mark T wrote: > > In article <3EE2FC42.63A0F747@chello.at>, Helmut Leitner says... > > > >When you write an innocent > > > > char buf[256]; > > .... > > printf("buf=%s\n",buf); > > > >You get an > > > > buf=Error: Access Violation > > > >You need to write > > > > printf("buf=%s\n",&buf[0]); > > > >to get the code to work correctly. > > > >Of course it's the same with > > fgets(&buf[0],buf.size,c.stdio.stdin); > >and dozens of other C functions that take char * (in fact array) > >parameters. > > > >This will cost a lot of progammers that are used to C > >a lot of time to get used to. > > To call C functions from Ada you have to do the same thing, explicitly pass the address of buf[0], which is not a bad thing since that clearly states what is going on. The C idiom of array name is equivalent to address of first element should probably not be carried forward into D since it will probably prevent good type checking. I do agree this should be strongly docummented in any language manuals. Remember D is not supposed to be a syntax and semantically compatible with C just easily interfaced. When you use a new language you have to get used to it's syntax and semantics. A few extra characters for clarity will not hurt anyone. Welcome to strongly typed languages. I don't mind to write fgets(&buf[0],buf.size,c.stdio.stdin); but silent compilation of fgets(buf,buf.size,c.stdio.stdin); with a resulting runtime error is unacceptable. -- Helmut Leitner leitner@hls.via.at Graz, Austria www.hls-software.com |
June 08, 2003 Re: &buf[0] problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Helmut Leitner | Seems like another argument for having a true string type... Sz. |
June 17, 2003 Re: &buf[0] problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Helmut Leitner | "Helmut Leitner" <helmut.leitner@chello.at> wrote in message news:3EE2FC42.63A0F747@chello.at... > When you write an innocent > > char buf[256]; > .... > printf("buf=%s\n",buf); > > You get an > > buf=Error: Access Violation > > You need to write > > printf("buf=%s\n",&buf[0]); > > to get the code to work correctly. To get it to work best in D, write it as: printf("buf = %.*s\n", buf); > Of course it's the same with > fgets(&buf[0],buf.size,c.stdio.stdin); It shouldn't be, as fgets() has a parameter prototype for the first argument. D will implicitly convert an array to a pointer, so this should work fine: fgets(buf, buf.length, c.stdio.stdin); It's only an issue with varargs ... parameters. > - support the normal array passing semantics for functions that > are "extern C". Since printf is extern C, that would make it impossible to print D arrays using it. |
June 18, 2003 Re: &buf[0] problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter |
Walter wrote:
> > Of course it's the same with
> > fgets(&buf[0],buf.size,c.stdio.stdin);
>
> It shouldn't be, as fgets() has a parameter prototype for the first argument. D will implicitly convert an array to a pointer, so this should work fine:
>
> fgets(buf, buf.length, c.stdio.stdin);
Fine that it's working now, it didn't with my last version (0.61).
--
Helmut Leitner leitner@hls.via.at Graz, Austria www.hls-software.com
|
Copyright © 1999-2021 by the D Language Foundation