basic question about adresses and values in structs

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 4 07:00:13 PDT 2014


On 09/04/2014 02:54 AM, nikki wrote:

 > a pointer variable to save an adres of a function, then dereferencing 
to use
 > it.

If possible, even in C, I would recommend using a 'function pointer' for 
that. However, there are cases where the signature of the function 
should be unknown to the code that is storing it so a void* is used. 
(Note that, as discussed on these forums in the past, void* has always 
been intended to be a data pointer. The fact that it works for function 
pointers is something we get as lucky accidents, which will most 
probably always supported by compilers and CPUs.)

Here is how D does function pointers:

   http://dlang.org/expression.html#FunctionLiteral

And a chapter that expands on those:

   http://ddili.org/ders/d.en/lambda.html

 > Now I am wondering when to use the ** ?

The simple answer is when dealing with the address of a type that is 
'void*' itself. In other words, there is nothing special about **: It 
appears as the type that is "a pointer to a pointer". Inserting spaces:

     int * p;      // A pointer to an int
     void* * q;    // A pointer to a void*

     // (untested)
     static assert (is (typeof(*p) == int));
     static assert (is (typeof(*q) == void*));

     int i;
     *p = i;    // Can store an int

     void* v;
     *q = v;    // Can store a void*

Ali



More information about the Digitalmars-d-learn mailing list