www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A strange charArray.ptr behavior

reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
given the function:

export void ceaser_enc(char* input, ref char* output);

this compiles:
     char* sezar = (new char[65]).ptr;
     ceaser_enc(key, sezar);

this does not compile:

     char[] sezar = new char[65];
     ceaser_enc(key, sezar.ptr);

by yielding: "cannot pass rvalue argument cast(char*)sezar of 
type char* to parameter ref char* output"

Why is sezar an rvalue in the second case?
Dec 02 2020
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/2/20 12:20 PM, Ferhat Kurtulmu=C5=9F wrote:
 given the function:
=20
 export void ceaser_enc(char* input, ref char* output);
=20
 this compiles:
  =C2=A0=C2=A0=C2=A0 char* sezar =3D (new char[65]).ptr;
  =C2=A0=C2=A0=C2=A0 ceaser_enc(key, sezar);
=20
 this does not compile:
=20
  =C2=A0=C2=A0=C2=A0 char[] sezar =3D new char[65];
  =C2=A0=C2=A0=C2=A0 ceaser_enc(key, sezar.ptr);
=20
 by yielding: "cannot pass rvalue argument cast(char*)sezar of type char=
*=20
 to parameter ref char* output"
=20
 Why is sezar an rvalue in the second case?
Not 'sezar' but sezar.ptr is an rvalue. Imagine ptr() being a function=20 that returns a value: T* ptr() { // ... } That pointer is an rvalue and D disallows binding them to 'ref' parameter= s. In the first case, 'sezar' is a local variable, which is an lvalue. Ali
Dec 02 2020
parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Wednesday, 2 December 2020 at 21:01:22 UTC, Ali Çehreli wrote:
 On 12/2/20 12:20 PM, Ferhat Kurtulmuş wrote:
 given the function:
 
 export void ceaser_enc(char* input, ref char* output);
 
 this compiles:
      char* sezar = (new char[65]).ptr;
      ceaser_enc(key, sezar);
 
 this does not compile:
 
      char[] sezar = new char[65];
      ceaser_enc(key, sezar.ptr);
 
 by yielding: "cannot pass rvalue argument cast(char*)sezar of 
 type char* to parameter ref char* output"
 
 Why is sezar an rvalue in the second case?
Not 'sezar' but sezar.ptr is an rvalue. Imagine ptr() being a function that returns a value: T* ptr() { // ... } That pointer is an rvalue and D disallows binding them to 'ref' parameters. In the first case, 'sezar' is a local variable, which is an lvalue. Ali
That makes sense. Thank you.
Dec 02 2020