www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - char[] ported from C to char[0] in the D core library

reply "badlink" <andrea.9940 gmail.com> writes:
The struct core.sys.linux.sys.inotify.inotify_event contains the 
field "char[0] name" which corresponds to "char name[]" in C.

Why it has been translated to "char[0]" ?
For me "char*" would have been more appropriate.
Sep 09 2015
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 9 September 2015 at 16:49:39 UTC, badlink wrote:
 The struct core.sys.linux.sys.inotify.inotify_event contains 
 the field "char[0] name" which corresponds to "char name[]" in 
 C.

 Why it has been translated to "char[0]" ?
In that structure, the name is appended directly to the end of the data instead of pointed to. The size isn't known at compile time, so a zero-length placeholder is there instead. You'd access it by using the .ptr property and get the length out of the `len` field.
 For me "char*" would have been more appropriate.
That's typically right, but since this case does it in-place instead of pointed, the zero length array is most accurate.
Sep 09 2015
parent "badlink" <andrea.9940 gmail.com> writes:
On Wednesday, 9 September 2015 at 16:59:09 UTC, Adam D. Ruppe 
wrote:
 That's typically right, but since this case does it in-place 
 instead of pointed, the zero length array is most accurate.
I didn't consider that the name is placed right after the struct. Thanks !
Sep 09 2015
prev sibling parent reply "Alex Parrill" <initrd.gz gmail.com> writes:
On Wednesday, 9 September 2015 at 16:49:39 UTC, badlink wrote:
 The struct core.sys.linux.sys.inotify.inotify_event contains 
 the field "char[0] name" which corresponds to "char name[]" in 
 C.

 Why it has been translated to "char[0]" ?
 For me "char*" would have been more appropriate.
It's a flexible array member [1], not a pointer. Changing it to `char*` would make it incompatible with the C functions using it. [1]: https://en.wikipedia.org/wiki/Flexible_array_member
Sep 09 2015
parent "badlink" <andrea.9940 gmail.com> writes:
On Wednesday, 9 September 2015 at 19:37:54 UTC, Alex Parrill 
wrote:
 It's a flexible array member [1], not a pointer. Changing it to 
 `char*` would make it incompatible with the C functions using 
 it.

 [1]: https://en.wikipedia.org/wiki/Flexible_array_member
TIL a new detail about C on the D forum ;)
Sep 10 2015