Friday, 30 March 2012

Introduction - Pointer


Introduction:


è    Pointer is a derived data type in C. It is built from the one of the fundamental data type available in C.
è    A pointer is a variable that points to or references a memory location in which data is stored.
è    Each memory cell in the computer has an address that can be used to access that location so a pointer variable points to a memory location(address). It can be used for different purpose such as access the variable value at that address , to increment it to get address of next element and change the contents of this memory location via the pointer.

 

Pointer declaration:

 

è    In C variable must be declared for its use. The poiner variable must be declared before it is used. A pointer is a variable that contains the memory location(address) of another variable.
è    The syntax is as shown below.
type * variable_name ;
è    According to the above syntax type specifying the type of data stored in the location identified by the pointer. The asterisk(*) tells the compiler that variable_name is a pointer variable. variable_name is the name of the variable.
è    The unary operator * is called indirection or dereferencing operator,
Ex,
int *ptr;
char *string;

Address operator:

 

è    Once declare a pointer variable must be need to point it to something it  can be done  by assigning to the pointer the address of the variable.
è    The unary operator & called address of operator is used to assign the address of variable.
Ex,     int num=10;
          int *ptr;       // Declaration
ptr=#  // Initialization

è    This places the address where num is stores into the variable ptr. If num is stored in memory 212 address then the variable ptr has the value 21260.

/* A program to illustrate pointer declaration*/
void main()
{
int *ptr;
int sum;
sum=45;
ptr=∑
printf (“\n Sum is %d  \n”, sum);
printf (“\n The address of sum  is = %u”, ptr);
printf(“\n value of sum is = %d”,*ptr);
}


Posted By :  Ruchita Pandya

No comments:

Post a Comment