newbie prob: referenced parameter only partially changed

torhu fake at address.dude
Sun May 20 14:14:54 PDT 2007


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 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.



More information about the Digitalmars-d mailing list