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 a int b= 15; //Declaring variable a int *p; //Declaring pointer variable p = &a; //Storing address of a variable in the pointer p = &b; //Storing address of b variable in the pointer // Address of a variable writeln("Address of a: ",&a); // Address of b variable writeln("Address of b: ",&b); // Address stored in pointer variable writeln("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.RELATED TAGS
CONTRIBUTOR
View all Courses