www.digitalmars.com         C & C++   DMDScript  

D - &buf[0] problem

reply Helmut Leitner <helmut.leitner chello.at> writes:
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
next sibling parent reply Mark T <Mark_member pathlink.com> writes:
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
parent reply Helmut Leitner <helmut.leitner chello.at> writes:
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
Jun 08 2003
parent "Luna Kid" <lunakid neuropolis.org> writes:
Seems like another argument for having a true string type...

Sz.
Jun 08 2003
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"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
parent Helmut Leitner <helmut.leitner chello.at> writes:
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
Jun 18 2003