newbie prob: referenced parameter only partially changed
J D
digitalmars at dresserfamily.org
Sun May 20 14:49:35 PDT 2007
torhu Wrote:
> J D wrote:
> > Love the language, from the description. My first day trying to program in D.
> >
>
> Nice to hear that. Here's a page that I found very useful when learning D:
> http://www.prowiki.org/wiki4d/wiki.cgi?LanguageSpecification/KeywordIndex
>
> > I pass a dynamic char array to a function. I modify the contents and set the .length to a smaller value (confirmed from within the routine). When I return the array I passed in has the contents changed, but the length has not changed. This seems inconsistent to me.
>
> The array reference consists of the length, and a pointer to the data.
> What you get inside the function is just a copy of this reference, in
> other words a different reference to the same data. Most of the time
> it's ok to just return a new reference if the function might change it.
I'll have to remember that, but it stil seems a wee bit inconsistent in that it changes the contents but not the length.
> >
> > I tried putting 'ref' before the 'char []' in the function declaration, but I got a bunch of errors,although the docs said arrays are passed by reference by default. Can anyone help?
>
> This should work. 'ref' was added not long ago, so maybe you compiler
> is too old. Try using 'inout' instead, which is an older alias for ref.
I am using gdc, and 'inout' worked so I guess it is not the newest.
I tried installing dmd too. Which is better? What are the trade-offs?
dmd would not compile the sample code. It complained of not being able to find libraries even though it was there.
Thanks much for the help. I have a feeling you'll be hearing more from me. :)
More information about the Digitalmars-d
mailing list