In D programming language, a pointer is a variable that points to the memory address of another variable. It can be defined in any datatype like int
, char
, double
, and so on. However, the pointer and the variable whose address is assigned to the pointer should have the same data type. For example, if the pointer is of int
data type, then the variable x
should also have int
data type.
Let's look at the syntax:
int *p;
*
with the variable.*
, and it will help to access the value stored on the address that the pointer points to.&
to get the address of that variable. Let's look at the code below:
import std.stdio;void main () {int a= 10; //Declaring variable aint b= 15; //Declaring variable aint *p; //Declaring pointer variablep = &a; //Storing address of a variable in the pointerp = &b; //Storing address of b variable in the pointer// Address of a variablewriteln("Address of a: ",&a);// Address of b variablewriteln("Address of b: ",&b);// Address stored in pointer variablewriteln("Pointer point's toward address: ",*p);}
int *p
with the name p
by using *
before the name of the variable.a
to the pointer variable p
.b
to the pointer variable p
.a
and b
variables.Free Resources