You are on page 1of 5

POINTERS

The pointer is the most important topic of the ‘C’ or C++.


These works on the memory directly. If we are talking about the
memory, we must know what type of memory we are using.

There are some type of memories in the computer.

α Register Memory : This memory resides within the CPU,


this is very small memory used for the internal
calculations of the CPU.
α Cache Memory : This memory is also small but
comparatively larger than the register memory. It is used
for increasing the speed of the CPU processing.
α Primary Memory : This memory is RAM (Random Access
Memory). Its working is explained earlier.
α Secondary Memory : Hard Disk, CDs, etc lie under this
category. These are very large in size. Their working is
also explained in the introduction.

In C, generally we uses RAM i.e. primary memory to store the


data. We can also use register or secondary memory also if
required depending upon the need.

So, if we are using any kind of memory, there is a memory


address for each and every block of memory. In computers,
some blocks of memory are formed by grouping some memory
units. Each memory block is assigned a memory address by
which that block is identified by the CPU. If we have kept some
data in a memory block and we need to fetch that data from
that memory, the memory address of the data is hit and the
data is fetched.

In C, the memory address of a data type can be seen by using


the pointers.

DECLARATION :

int *p;
here, the * sign represents that p is of pointer type. This pointer
is declared as int hence, it is of integer type and can contain
the memory address of any variable of integer type.

Let us understand this with a example:

#include<stdio.h>
#include<conio.h>

void main()
{
int a, *p;
clrscr();

a = 10;
p = &a;
printf(“The value of a = %d”,a);
printf(“\nThe address of a is = %u”,&a);
printf(“\nThe address of a = %u”,p);
printf(“\nThe value of a is = %d”,*p);

getch();
}

Let’s understand this program now. We have taken a variable a


of integer type and given value of 10. Also, a pointer p is
declared of integer type only as a pointer of integer type can
store the address of a variable of integer variable only.
Remember that a pointer can only contain an address of a
variable, it can never have a user defined value. To proceed
further, I need to explain two more things.

& represents the address of notation i.e. if we say &a, it


represents the address of a. Address of a represents the
memory address of a where a is stored. To print an address
over the screen %u notation is used. See printf statement no. 2,
here I have printed the address of variable a using %u.
* represents the value of representation. In the above
program, I have given the address of a in pointer p by the
statement

P = &a;

Now, p contains the memory address of a. To show the address


of a we can directly print p over the screen but to show the
value of a using the pointer we must use *p as it represents:
the value stored at the address stored in p.

Let us understand this with the help of a diagram.

a p

10 2568

2568 4298

Here, the notations are represented as:

Variable Name

Value

Address

The above diagram explains the previous program. A variable a


having a memory address of 2568 has been assigned a value of
10. The pointer p contains the address of a and have its own
memory address as 4298. Note that, the pointer variable p is
also of integer type hence, it will also occupy some memory i.e.
2 bytes in the case of integer and contain its own memory
address also. We can also print the address of p using the %u
and showing &p.
SECOND DEGREE POINTER

Second degree pointer is a pointer that contains the address of


a pointer having the address of a variable. In the above
example, if we can have the address of p in another pointer q
than q will be called as second degree pointer. Like this, a
pointer can have any degree, depending upon the sequence.

Here is the example of second degree pointer:

#include<stdio.h>
#include<conio.h>

void main()
{
int a, *p, **q;
clrscr();

a = 10;
p = &a;
q = &p;

printf(“The value of a = %d”,a);


printf(“\nThe address of a is = %u”,&a);
printf(“\nThe address of a = %u”,p);
printf(“\nThe value of a is = %d”,*p);
printf(“The value of a = %d”,**q);
printf(“\nThe address of a is = %u”,*q);
printf(“\nThe address of p = %u”,&p);
printf(“\nThe address of p = %u”,q);
printf(“\nThe address of q = %u”,&q);
printf(“\nThe address of p = %u”,*&*&q);

getch();
}

Like this, we can use any notation.


Pointers will also be used in the further chapters but till
now, this much is enough. These will also be used in the arrays,
functions, structures etc.

You might also like