Thread overview
.ptr and .value
May 05, 2010
strtr
May 05, 2010
Robert Clipsham
May 05, 2010
strtr
May 05, 2010
Robert Clipsham
May 05, 2010
bearophile
May 05, 2010
Robert Clipsham
Mar 29, 2018
Timoses
May 05, 2010
I keep on expecting .ptr to work on all types. What keeps this from being the case? And how about having a .value(.deref) property for known pointers?

I feel a lot less comfortable with using stars and ampersands, because I keep on forgetting which one does what.
May 05, 2010
On 05/05/10 23:20, strtr wrote:
> I keep on expecting .ptr to work on all types. What keeps this from being the case?
> And how about having a .value(.deref) property for known pointers?
>
> I feel a lot less comfortable with using stars and ampersands, because I keep on forgetting which one does what.

.ptr is only available for arrays. Internally, (dynamic) arrays in D look like this:
----
struct {
  size_t length;
  T* ptr;
}
----
Where T is the type of the array. This is why you can use .ptr on it. For other types they are types on their own, so don't have such properties. As for .value and .deref, these won't be implemented, you'll have to live with & and * if you want to use pointers. Note that if you're using pointers you should be comfortable with them, if you're not it's probably best to avoid them. In fact, D makes it easy to do so!

As for referencing/dereferencing:
----
int myInt = 6;
int* ptrToMyInt = &myInt; // & takes a reference to myInt, making
                          // this a pointer
int myInt2 = *myInt; // myInt2 == 6, * dereferences.
----

Again, let me reinforce that you don't need to use pointers in D, they're easy to avoid in most cases... Feel free to play with them, and as you gain confidence you may find uses for them :) When I first learned how to use pointers I found this little video/tutorial a fun intro to them, you might like to take a look: http://cslibrary.stanford.edu/104/ :)
May 05, 2010
Robert Clipsham Wrote:

> On 05/05/10 23:20, strtr wrote:
> > I keep on expecting .ptr to work on all types. What keeps this from being the case? And how about having a .value(.deref) property for known pointers?
> >
> > I feel a lot less comfortable with using stars and ampersands, because I keep on forgetting which one does what.
> 
> .ptr is only available for arrays. Internally, (dynamic) arrays in D look like this:
> ----
> struct {
>    size_t length;
>    T* ptr;
> }
> ----
> Where T is the type of the array. This is why you can use .ptr on it. For other types they are types on their own, so don't have such properties. As for .value and .deref, these won't be implemented, you'll have to live with & and * if you want to use pointers. Note that if you're using pointers you should be comfortable with them, if you're not it's probably best to avoid them. In fact, D makes it easy to do so!
I actually made this a while ago :) http://bayimg.com/NaeOgaaCC

> 
> As for referencing/dereferencing:
> ----
> int myInt = 6;
> int* ptrToMyInt = &myInt; // & takes a reference to myInt, making
>                            // this a pointer
> int myInt2 = *myInt; // myInt2 == 6, * dereferences.
> ----

But wouldn't this (property sugar?) be nice?

int myInt = 6;
int* ptrToMyInt = myInt.ptr;
int myInt2 = ptrToMyInt.deref; // you probably didn't mean *myInt ;)

> Again, let me reinforce that you don't need to use pointers in D,
Especially when interfacing with c libs, I'm forced to use *& often :(
Also *this is necessary to deref the this pointer within structs and classes.
On the other hand I don't remember using ** or && for quite some time.

> they're easy to avoid in most cases... Feel free to play with them, and as you gain confidence you may find uses for them :) When I first learned how to use pointers I found this little video/tutorial a fun intro to them, you might like to take a look: http://cslibrary.stanford.edu/104/ :)

I understand the concept of pointers, but I just keep forgetting which one to use * or &. probably because  I don't like the double * meaning.

May 05, 2010
Robert Clipsham:

> .ptr is only available for arrays.

And arr.ptr returns the pointer to the start of the memory that contains the array data. While &arr is the pointer to the struct that contains the pointer and the length.


> As for .value and .deref, these won't be implemented, you'll have to live with & and * if you want to use pointers. Note that if you're using pointers you should be comfortable with them, if you're not it's probably best to avoid them. In fact, D makes it easy to do so!

On the other hand I think the Pascal pointer syntax is better :-)

Bye,
bearophile
May 05, 2010
On 05/05/10 23:58, strtr wrote:
> But wouldn't this (property sugar?) be nice?

I don't like it, I can see why you would though :)

> int myInt = 6; int* ptrToMyInt = myInt.ptr; int myInt2 =
> ptrToMyInt.deref; // you probably didn't mean *myInt ;)

I guess this is what I get for writing code quickly without checking it
on newsgroups ;) No wonder I gave in and wrote patches for dmd's debug
info :D

> I understand the concept of pointers, but I just keep forgetting
> which one to use * or&. probably because  I don't like the double *
> meaning.

It takes a while to get used to if you're not used to it, you get used to it after a while though :)
May 05, 2010
On 06/05/10 00:11, bearophile wrote:
> Robert Clipsham:
>
>> .ptr is only available for arrays.
>
> And arr.ptr returns the pointer to the start of the memory that
> contains the array data. While&arr is the pointer to the struct that
> contains the pointer and the length.

Thanks for adding this, I probably should have mentioned it :)

> On the other hand I think the Pascal pointer syntax is better :-)

I've never used Pascal, so I wouldn't know :)

> Bye, bearophile

March 29, 2018
On Wednesday, 5 May 2010 at 22:42:19 UTC, Robert Clipsham wrote:
> .ptr is only available for arrays. Internally, (dynamic) arrays in D look like this:
> ----
> struct {
>   size_t length;
>   T* ptr;
> }
> ----

Thanks for this!! (I know this topic is old).
But it made me understand why I found that this is working correctly:

  class Storage(T)
  {
    T type;

    this(ref ubyte[] stream)
    {
      static if (isArray!T)
        this.type = cast(T*)(&stream);    // because the array should actually
                                          // point to an array
      else
        this.type = cast(T*)(stream.ptr); // because the data type T
                                          // should point to the actual data
    }
  }

I wonder if the difference of '&' and '.ptr' could be worth mentioning here
https://dlang.org/spec/arrays.html

...