What is RAM
To understand pointer, you should know about the primary
memory (RAM). RAM stand for Random Access Memory means that any memory location
of RAM can be accessed randomly. When you open a program to run, it loads from storage
to RAM. It means that all running programs load on RAM. When a running program
is closed, it is unloaded from RAM. RAM contains a number of memory locations
according to its size. Each memory location has its unique memory address used
to access that location. Size of each memory address is 32 bit (4 bytes).
How to access a particular memory address of RAM
Let us understand it with an examples.
Example 1:
Suppose you have to send a gift to a parson named John. You
request someone to take that parcel. Here point to reference is a name.
However, you can tell the complete address of John as street no, house no. Here
point of reference is address of house. It means that you have two ways to locate
house.
Example 2:
Suppose, hundreds of people are sitting in an auditorium.
The host is going to announce a prize for a person among the audience. There
are two methods to call the prizewinner to dais. The host can either call the name of the
person or the number of the seat. These are equivalent to ‘call by name’ and
‘call by address’ methods. In both cases, the prize will be delivered to a
person whether he is called by name or referred by address (seat number in this
case).
Similarly, you can access a variable x by two ways, by
variable name and by memory address where memory is reserved by variable x. A
variable is used to store value and a pointer is used to store memory address
of memory location where value of variable is stored.
C++ Pointers
Pointer is special type of variable in which memory address
is stored. Using memory address stored in pointer, you can get value stored at
that memory address.
Declaring a Pointer
Pointers works by pointing a particular data type as integer
pointer points to integer data type, double pointer points to double data type
and so on. Syntax of pointer declaration is
data-type *pointer-name;
Here pointer-name is name of pointer, data-type is the type
of data to which the pointer points. There is no space between asterisk (*) and
the pointer name. Declaration of pointers to different data types are given
below
Multiple pointers of same data type can be declared in a
single as
Moreover, we can declare variables, arrays and pointers of
same data type in a single line.
- number is variable of data type double
- ptr is pointer to data type double
- a is array of data type double
Address operator / Referencing operator
Let us declare a variable x and initialize it with value 10.
Here x is the name of memory location where 10 is stored. To get memory address
of this memory location (named x), we use Address operator (Ampersand sign &).
We declare an int pointer ptr and store memory address in ptr.
Here ptr contains the memory address of memory location
named x where value 10 is stored.
Dereference operator
Dereference operator (asterisk * sign with pointer) is used with pointer ptr to get value from memory address stored in ptr as
Here don’t confuse between int *ptr and *ptr
- Int *ptr is declaration of pointer named ptr
- *ptr is dereferencing of pointer ptr (Means getting value from memory address stored in ptr)
Two ways to get value of memory location named x
By name
By reference
Now we demonstrate above concepts practically as
Below is C++ program to cover above pointer concepts
Pointer basic in c++ |
Pointer basic in c++ output |
Post a Comment