www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - .ptr and .value

reply strtr <strtr spam.com> writes:
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
parent reply Robert Clipsham <robert octarineparrot.com> writes:
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
next sibling parent reply strtr <strtr spam.com> writes:
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!

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, 

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
parent Robert Clipsham <robert octarineparrot.com> writes:
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
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent Robert Clipsham <robert octarineparrot.com> writes:
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

May 05 2010