newbie prob: referenced parameter only partially changed
Downs
default_357-line at yahoo.de
Sun May 20 14:10:17 PDT 2007
J D wrote:
> Love the language, from the description. My first day trying to program in D.
>
> 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.
>
> 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?
It helps to think of arrays as structs made of a length and a pointer (because that's what they are, really ^^ )
So when you pass an array in by value and change its contents, the length that you're changing belongs to a copy of the array that the function was called with.
ref char[] should work, but that's a recent change and might not be supported by your compiler yet. Try inout instead of ref.
Finally, can you paste your code? We can't help you properly until we can reproduce the problem.
-- downs
More information about the Digitalmars-d
mailing list