Pointers


Computers use their memory for storing the instructions of a program, as well as the values of the variables that are associated with it. The computer’s memory is sequential collection of "storage cells". Each cell is commonly known as a "byte" and has a number called "address" associated with it.
Whenever we declare a variable, the system allocates, somewhere in the memory, an appropriate location to hold the value of the variable. Since, every byte has a unique address no; this location will have its own address no.

Definition of pointers:

A pointer is a variable that contains an address which is a location of another variable in memory. Since the memory addresses are simply no.s they can be assigned to some variables, which can be stored in memory, like any other variable. Such variables that hold memory address are called pointers.

Declaration of pointers:

in ‘c’, every variable must be declared for its type. Since pointer variables contain addresses that belong to a separate data type, they must be declared as pointers before we use them. The declaration of a pointer variable takes the following form:

Datatype    *pointer name

This tells the compiler three things about pointer name:

  1. The asterisk (*) tells the variable ‘ptrname’ is a pointer variable.
  2. ‘Ptrname’ needs a memory location.
  3. ‘Ptrname’ points to a variable of type datatype.


For example:
1) int *p;
It declares the variable ‘p’ as a pointer variable points to an integer datatype.
NOTE: The type ‘int’ refers to the data type of the variable being pointed by ‘p’ and not the type of value of the pointer.

2) float *q;
It declares the variable ‘q’ as a pointer variable that points to float datatype.

3) char *ch;
It declares ‘ch’as a pointer to a character variable.

INITIALISATION OF POINTERS:

Once a pointer variable has been declared, it can be made to point to a variable using an assignment statement. This is known as "pointer initialization".

Ex:
Suppose an integer variable ‘i’ and a pointer variable ‘x’ of integer type is declared.
i.e., int i,*x;
Now integer pointer can be initialized as;

x=&i;
which causes ‘x’ to point to ‘i’.
i.e., ‘x’ contains address of ‘i’.


NOTE: Before a pointer is initialized, it should be used.
We must ensure that the pointer variables always point to the corresponding type of data.
A pointer variable can be initialized in its declaration itself.

int i, *x=&i;

Ex:
It declares ‘i’ as an integer variable and ‘x’ as a pointer variable and then initializes ‘x’ to the address of ‘i’.

NOTE: The above is an initialization of ‘x’, but not *x.
The target variable ‘i’ must be declared first.
The following statement is invalid.

int *x=&i, i;

As it is mentioned above, the pointer variables always point to the corresponding type of data, care should be taken to avoid wrong pointer assignments.

Ex:
float a, b;
int x, *p;
p=&a;
b=*p;
will result in erroneous output.

ACCESSING A VARIABLE THROUGH ITS PIONTER

We can access the value of a variable using pointer by using a unary operator *(asterisk), usually known as indirection operator.
‘x’ is also known as ‘value at address’ operator.
Consider the following statements.

int i, *p, j;
i=10;
p=&i;
j=*p;

The first line declares: i&j as integer variables and ‘p’ as a pointer variable pointing to an integer .

The second line assigns the value 10 to ‘i’.
The third statement assigns the address of ‘i’ to the indirection operator *.
When the operator * is placed before a pointer variable in an expression, the pointer returns the value variable of which the pointer value is the address.
In the above example *p returns the value of variable ‘i’.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
	{
	   int a=10;
	   int *b;
	   b=&a;
	   printf("value of a=%d \n",a);
	   printf("value of a=%d \n",*(&a));
	   printf("value of a=%d \n",*b);
	   printf("address of a=%u \n",&a);
	   printf("address of a=%u \n",b);
	   printf("address of b=%u \n",&b);
	   printf("value of b=address of a=%u \n",b);
	   getch();
      }

O/P
Value of a=10
Value of a=10
Value of a=10
Address of a=5000
Address of a=5000
Address of b=3000
Value of b=address of a=5000