string to char array?
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jun 5 12:18:38 PDT 2015
On Friday, 5 June 2015 at 17:27:18 UTC, Kyoji Klyden wrote:
> On Thursday, 4 June 2015 at 22:28:50 UTC, anonymous wrote:
[...]
>> By the way, there are subtly different meanings of "array" and
>> "string" which I hope you're aware of, but just to be sure:
>> "array" can refer to D array types, i.e. a pointer-length
>> pair, e.g. char[]. Or it can refer to the general concept of a
>> contiguous sequence of elements in memory.
>> And as a special case, "string" can refer to D's `string`
>> type, which is an alias for `immutable(char)[]`. Or it can
>> refer to a contiguous sequence of characters in memory.
>> And when ketmar writes: "it's a pointer to array of pointers
>> to first chars of strings", then "array" and "string" are
>> meant in the generic way, not in the D-specific way.
>
[...]
>
> So how does D store arrays in memory then?
>
> I know you already explained this part, but..
> Does the slice's pointer point to the slice's position in
> memory? Then if an array isn't sequential, is it atleast a
> sequence of pointers to the slice structs (& those are just in
> whatever spot in memory they could get?)
> There's a slice for each array index..right? Or is it only for
> the first element?
Oh boy, I think I might have put more confusion on top than
taking away from it.
I didn't mean to say that D's arrays are not sequential. They
are. Or more specifically, the elements are. "Array" is often
meant to mean just that: a contiguous sequence of elements in
memory. This is the same in C and in D.
If you have a pointer to such a sequence, and you know the number
of elements (or what element is last), then you can access them
all.
In C, the pointer and the length are usually passed separately.
But D groups them together and calls it a "dynamic array" or
"slice": `ElementType[]`. And they're often simply called
"arrays". This is done to confuse newbies, of course.
There's an article on slices (dynamic arrays):
http://dlang.org/d-array-article.html
C doesn't know about D's slice structure, of course. So when
talking to C, it's often necessary to shuffle things around.
For example, where a D function has a parameter `char[] arr`, a C
version may have two parameters: `size_t length, char*
pointer_to_first`. And if you want to call that C version with a
D slice, you'd do `cfun(dslice.length, dslice.ptr)`.
It gets interesting with arrays of arrays. In D that would be
`char[][] arr`. And in C it could be `size_t length, char**
pointers_to_firsts, size_t* lengths`.
Now what to do? A `char[][]` refers to a sequence of D slices,
but the C function expects two sequences: one of pointers, and
one of lengths. The memory layout is incompatible. You'd have to
split the `char[][]` up into two arrays: a `char*[] ptrs` and a
`size_t[] lengths`, and then call the function as
`cfun(ptrs.length, ptrs.ptr, lengths.ptr)`
(Hope I'm not makings things worse with this.)
More information about the Digitalmars-d-learn
mailing list