D - &buf[0] problem
- Helmut Leitner (25/25) Jun 08 2003 When you write an innocent
- Mark T (10/26) Jun 08 2003 To call C functions from Ada you have to do the same thing, explicitly p...
- Helmut Leitner (9/45) Jun 08 2003 I don't mind to write
- Luna Kid (2/2) Jun 08 2003 Seems like another argument for having a true string type...
- Walter (11/24) Jun 17 2003 To get it to work best in D, write it as:
- Helmut Leitner (5/13) Jun 18 2003 Fine that it's working now, it didn't with my last version (0.61).
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
Jun 08 2003
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.
Jun 08 2003
Mark T wrote:In article <3EE2FC42.63A0F747 chello.at>, Helmut Leitner says...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.comWhen 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.
Jun 08 2003
Seems like another argument for having a true string type... Sz.
Jun 08 2003
"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.
Jun 17 2003
Walter wrote: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.comOf 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);
Jun 18 2003