Pointers are symbolic representation of addresses.

pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. 

Syntax:

type *var-name;

1.WAP to program to illustrate Pointers in C.

#include <stdio.h> void main() { int var = 20; // declare pointer variable int *ptr; // note that data type of ptr and var must be same ptr = &var; // assign the address of a variable to a pointer printf("Value at ptr = %p \n",ptr); printf("Value at var = %d \n",var); printf("Value at *ptr = %d \n", *ptr); }

Output:






We can use the Pointer by two method:

1.Call By Value: Passing a Copy of Variable

2.Call By Reference: Passing a address of Variable.


1.Call By Value:

In this method we pass a copy of the variable and not the actual variable to the called function. So, any change made to the copy of the variable in the called function is not reflected back to the original variable.

Example:

#include<stdio.h>
void main()
{
    int *p; //pointer Initilization
    int c;  
    int a[3]={1,25,3,4};
    c = &a[0]; // Accessing the value of array a with index 0
    p=a+1;
    printf("%d",*p); // Displaying value at pointer p 
}
Output:





2.Call By Reference:

In this case we pass the address of the variable as argument to the function. So, any change made to the addressed variable in the called function is reflected back to the original variable.

Example:
#include<stdio.h>
void add(int*p,int*s)
{
    *p+=10;
    *s-=10;
}
void main()
{
    int *p,a=3,c=20;
    add(&a,&c); //calling function
    printf("%d %d",a,c);
}
Output: