example of pointer usefulness in D

Ali Çehreli via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 21 12:56:06 PDT 2014


On 10/21/2014 05:22 AM, edn wrote:> Could someone provide me with 
examples showing the usefulness of
 > pointers in the D language? They don't seem to be used as much as in C
 > and C++.

A pointer is the only sensible option for the following:

- References to any local data because 'ref' is only for parameters and 
return types.

     int a;
     int b;
     int* r = (condition ? &a : &b);    // r must be a pointer
     *r = 42;

- Any link in a linked data structure like linked lists and trees:

struct L
{
     L* next;    // must be a pointer
}

One can use classes and slices for references as well but they are more 
expensive.

Ali



More information about the Digitalmars-d mailing list