www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C style array declaration.

reply Aedt <adnansignsup gmail.com> writes:
I'm a big fan of betterC. In C, you can initialize an array 
without specifying the length like this
int ia[ ] = {0, 2, 1};

What is the translation of this? Note that int[] is a different 
type than C's arrays. 
https://dlang.org/spec/interfaceToC.html#data_type_compat says 
there are no equivalent to this. What's the workaround?

Also, is it possible to retrieve the pointer of the sequence of 
actual data from std.container.array? If all fails I'd like to 
use this container.
Mar 26 2018
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 26/03/2018 11:16 PM, Aedt wrote:
 I'm a big fan of betterC. In C, you can initialize an array without 
 specifying the length like this
 int ia[ ] = {0, 2, 1};
 
 What is the translation of this? Note that int[] is a different type 
 than C's arrays. 
 https://dlang.org/spec/interfaceToC.html#data_type_compat says there are 
 no equivalent to this. What's the workaround?
 
 Also, is it possible to retrieve the pointer of the sequence of actual 
 data from std.container.array? If all fails I'd like to use this container.
Scroll further down. C: T[] -> D: T* https://dlang.org/spec/interfaceToC.html#passing_d_array
Mar 26 2018
prev sibling next sibling parent ag0aep6g <anonymous example.com> writes:
On 03/26/2018 12:16 PM, Aedt wrote:
 I'm a big fan of betterC. In C, you can initialize an array without 
 specifying the length like this
 int ia[ ] = {0, 2, 1};
 
 What is the translation of this?
The language doesn't have that feature. But there's a PR to add `staticArray` to the standard library [1]. When that gets in, you can write: import std.array; auto ia = [0, 2, 1].staticArray; /* ia is an int[3] */ [...]
 Also, is it possible to retrieve the pointer of the sequence of actual 
 data from std.container.array? If all fails I'd like to use this container.
Take the address of the first element: import std.container.array: Array; Array!int a = [1, 2, 3]; int* p = &a[0]; assert(*p == 1); assert(*++p == 2); assert(*++p == 3); Be aware that the pointer potentially becomes invalid when you append to the array. [1] https://github.com/dlang/phobos/pull/6178
Mar 26 2018
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 26 March 2018 at 10:16:55 UTC, Aedt wrote:
 I'm a big fan of betterC. In C, you can initialize an array 
 without specifying the length like this
 int ia[ ] = {0, 2, 1};
That's the equivalent of D's static arrays if a variable, and is passed to C functions as a pointer. So important to note it is two different beasts in different contexts. I recently wrote about this here: https://forum.dlang.org/thread/uhibzcfwzqzqqfbrckup forum.dlang.org so read that post for some more detail, especially with relation to `extern` variables.
 Also, is it possible to retrieve the pointer of the sequence of 
 actual data from std.container.array? If all fails I'd like to 
 use this container.
address of first element for that, but if you are using a built-in D array, you can also just `arr.ptr`.
Mar 26 2018