POINTER

  Pointer is a variable that stores the address of another variable. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data.

C Pointer is used to allocate memory dynamically i.e. at run time. The variable might be any of the data type such as int, float, char, double, short etc.

Syntax : Pointers require a bit of new syntax because when you have a pointer, you need the ability to both request the memory location it stores and the value stored at that memory location.
data_type *ptr_name;

Example :
int *a; char *a;
Where, * is used to denote that ''a'' is pointer variable and not a normal variable.

Key points to remember about pointers in C:

# Normal variable stores the value whereas pointer variable stores the address of the variable.

# The content of the C pointer always be a whole number i.e. address.

# Always C pointer is initialized to null, i.e. int *p = null.

# The value of null pointer is 0.

# & symbol is used to get the address of the variable.

# * symbol is used to get the value of the variable that the pointer is pointing to.

# If pointer is assigned to NULL, it means it is pointing to nothing.

# Two pointers can be subtracted to know how many elements are available between these two pointers.

# But, Pointer addition, multiplication, division are not allowed.

# The size of any pointer is 2 byte (for 16 bit compiler).

Example program for pointer in C:

#include
int main()
{
int *ptr, q;
  q = 50;

  /* address of q is assigned to ptr */
  ptr = &q;

  /* display q's value using ptr variable */
  printf("%d", *ptr);
  return 0;
}

NULL POINTERS: 
It is always a good practice to assign a NULL value to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer. Eg : int *ptr = NULL;
The value of ptr is 0

POINTER ARITHMETIC: 

As you understood pointer is an address which is a numeric value; therefore, you can perform arithmetic operations on a pointer just as you can a numeric value. There are four arithmetic operators that can be used on pointers: ++, --, +, and -.

Example :
ptr++;
ptr--;
ptr+21;
ptr-10;

If a char pointer pointing to address 100 is incremented (ptr++) then it will point to memory address 101

POINTERS VS ARRAYS 

Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable in many cases. For example, a pointer that points to the beginning of an array can access that array by using either pointer arithmetic or array-style indexing.

int main ()
{
int var[3] = {1, 2, 3};
int *ptr;
printf("%d \n",*ptr);
ptr++;
printf("%d \n",*ptr);
return 0;
}

this code will return :
1
2

POINTER TO POINTER: 

A pointer to a pointer is a form of multiple indirection or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value. int main ()
{
int var;
int *ptr;
int **pptr;
var = 3000;
ptr = &var;
pptr = &ptr;
printf("Value of var :%d \n", var);
printf("Value available at *ptr :%d \n",*ptr);
printf("Value available at **pptr :%d\n",**pptr);
return 0;
}

this code will return
Value of var :3000
Value available at *ptr :3000
Value available at **pptr :3000
 

           1.ADDITION OF TWO NUMBER:

&b;
r=#include<stdio.h>
int main()
{
int a,b=20,c=30;
int *p,*q,*r;
p=&a;
q=&c;
*p=*q+*r;
printf("Add=%d",*p);
}

•OUTPUT 

Add=50

               2.USER INPUT IN POINTER: 

#include<stdio.h>
int main()
{
int a,b,c;
int *p,*q,*r;
p=&a;
q=&b;
r=&c;
printf("Enter first number\n");
scanf("%d",q);
printf("Enter second number\n");
scanf("%d",r);
*p=*q+*r;
printf("Add=%d",*p);
}

•OUTPUT

Enter first number
65
Enter second number 
45
Add=110

         3.SUBTRACTION OF TWO NUMBER:

#include<stdio.h>
int main()
{
int a,b=50,c=30;
int *p,*q,*r;
p=&a;
q=&b;
r=&c;
*p=*q-*r;
printf("Sub=%d",*p);
}

•OUTPUT 

Sub=20

        4.MULTIPLICATION OF TWO NUMBER:

#include<stdio.h>
int main()
{
int a,b=20,c=30;
int *p,*q,*r;
p=&a;
q=&b;
r=&c;
*p=*q**r;
printf("Multiply=%d",*p);
}
      
•Output 

Multiply =700

            5.DIVISION OF TWO NUMBER:

#include<stdio.h>
int main()
{
float a,b=210,c=30;
float *p,*q,*r;
p=&a;
q=&b;
r=&c;
*p=*q/*r;
printf("Div=%f",*p);
}

•OUTPUT 

Division =6

                     6.TABLE OF ONE:

#include<stdio.h>
int main()
{
int i=1;
int *p;
p=&i;
while(*p<=10)
{
printf("%d\n",*p);
(*p)++;
}
}

•OUTPUT 

1
2
3
4
5
6
7
8
9
10

                7.POINTER TO ARRAY:

#include<stdio.h>
int main()
{
int ar[10]={10,20,50,40,60,80,70,25,45,65};
for(int i=0;i<=9;i++)
{
printf("%d\n",*(ar+i));
}
}

•OUTPUT 

10
20
50
40
60
80
70
25
45
65

             8.POINTER TO FUNCTION: 

#include<stdio.h>
void area(int *h,int *w)
{
int ar=*h * *w;
printf("Area of rectangle=%d",ar);
}
int main()
{
int x,y;
printf("Enter height\n");
scanf("%d",&x);
printf("Enter width\n");
scanf("%d",&y);
area(&x,&y);
}

•Output 

Enter height 
12
Enter width 
13
Area of rectangle =156

            9.POINTER TO STRUCTURE: 

#include<stdio.h>
struct Student
{
int rollno;
};
int main()
{
struct Student obj_roll;
obj_roll.rollno=205;
struct Student *r,*m;
r=&obj_roll;
printf("Roll number=%d\n",*r);
}

•OUTPUT 

Roll number =205

        10.PRINTING VALUES IN VARIABLES                              USING POINTER :

main()
{
int i,*j,**k;
i=45;
j=&amp;i;
k=&amp;j;
printf("Values in variables\n");
printf("Value in i %d\n",i);
printf("Value in j which is the address of i %d\n",j);
printf("Value in k which is the address of j %d\n",k);
//Lets see what is the real address of i,j,k
printf("Addresses\n ");
printf("Address of i which is value in j %u\n",&amp;i);
printf("Address of j which is the value in k %u\n",&amp;j);
printf("Address of k %u\n",&amp;k);

//printing using pointer
printf("Using Pointers\n");
printf("Value in i %d\n",*(&amp;i));
printf("Value in i %d\n",*j);
printf("Value in i %d\n",**k);
printf("Value in j %d\n",*(&amp;j));
printf("Value in j %d\n",*k);
printf("Value in k %d\n",*(&amp;k));
}

•Output 

Values in variables
Value in i 45
Value in j which is the address of i 6356780
Value in k which is the address of j 6356776
Addresses
Address of i which is value in j 6356780
Address of j which is the value in k 6356776
Address of k 6356772
Using Pointers
Value in i 45
Value in i 45
Value in i 45
Value in j 6356780
Value in j 6356780
Value in k 6356776

                11.SWAP TWO NUMBERS: 

main( )
{
int a,b;
printf("Enter a and b values\n");
scanf("%d%d",&amp;a,&amp;b);
printf ("Values before Swap\na=%d\nb=%d\n",a,b ) ;
swap(&amp;a,&amp;b ) ;
printf ("Values after Swap\na=%d\nb=%d\n",a,b ) ;
}
void swap( int *x, int *y )
{
int t ;
t = *x ;
*x = *y ;
*y = t ;
}

•OUTPUT 

Enter a and b values
30
35
Values before Swap
a=30
b=35
Values after Swap
a=35
b=30

   12.COPY ONE STRING TO OTHER STRING:

main()
{
  char s[25],*t;
  printf("Enter a string\n");
  scanf("%s",&amp;s);
  t=s;//copying base address of string
  while(*t!='\0')
  {
   printf("%c",*t);
   t++;
  }
  printf("\n");
}

•Output 

Enter a string
Hello
Hello

         13.CONCATENATE TWO STRINGS:

main()
{
  char s1[25],s2[25],*t1,*t2;
  int len1=0,len2=0,i;
  printf("Enter a string 1\n");
  scanf("%s",&amp;s1);
  printf("Enter a string 2\n");
  scanf("%s",&amp;s2);
  t1=s1;//copying base address of string
  t2=s2;
  while(*t1!='\0')
  {
   t1++;
   len1++;
  }
 //copying second string to first
  while(*t2!='\0')
  {
   *t1=*t2;
   t1++;
   t2++;
   len2++;
  }
  *t1='\0';
  i=0;
  //taking total string to initial base address
  while(i&lt;len1+len2)
  {
   i++;
   t1--;
  }

    printf("Concatenated String is ");

    while(*t1!='\0')
    {
     printf("%c",*t1);
     t1++;
    }

printf("\n");
}

•OUTPUT 

Enter a string 1
Hello
Enter a string 2
World
Concatenated String is HelloWorld

              14.COMPARE TWO STRINGS:

main()
{
  char s1[25],s2[25],*t1,*t2;
int cmp=0;
  printf("Enter a string 1\n");
  scanf("%s",&amp;s1);
  printf("Enter a string 2\n");
  scanf("%s",&amp;s2);
  t1=s1;//copying base address of string
  t2=s2;
  while(*t1!='\0'&amp;&amp;*t2!='\0')
  {
    if(*t1!=*t2)
    {
     cmp++;
    }
    t1++;
    t2++;
  }

  if(cmp==0)
  {
   printf("Both are equal\n");
  }
  else
  {
   printf("Both are not equal\n");
  }
}

•Output 

Enter a string 1
Hello
Enter a string 2
World
Both are not equal

         15.ADDITION OF TWO MATRIX USING                                     POINTER: 

main()
{
 int i,j,rows,col;
 printf("Enter number of rows\n");
 scanf("%d",&amp;rows);
  printf("Enter number of columns\n");
 scanf("%d",&amp;col);

 int a1[rows][col],a2[rows][col],add[rows][col];
 //Taking input for 1st matrix
 printf("Enter Matrix 1\n");
 for(i=0;i&lt;rows;i++)
 {
  for(j=0;j&lt;col;j++)
  {
   scanf("%d",(*(a1+i)+j));
  }
 }
  //Taking input for 2nd matrix
  printf("Enter Matrix 2\n");
 for(i=0;i&lt;rows;i++)
 {
  for(j=0;j&lt;col;j++)
  {
   scanf("%d",(*(a2+i)+j));
  }
 }
 //Addition of matrix
 for(i=0;i&lt;rows;i++)
 {
  for(j=0;j&lt;col;j++)
  {
   *(*(add+i)+j)=*(*(a1+i)+j)+*(*(a2+i)+j);
  }
 }

 printf("Addition of above matrices is\n");

  for(i=0;i&lt;rows;i++)
 {
  for(j=0;j&lt;col;j++)
  {
   printf("%d\t",*(*(add+i)+j));
  }

  printf("\n");
 }

}

•OUTPUT 

Enter number of rows
2
Enter number of columns
2
Enter Matrix 1
30
35
21
56
Enter Matrix 2
12
99
76
55
Addition of above matrices is
42      134
97      111
    
        16.SUBSTRACTION OF TWO MATRIX                                 USING POINTER: 

main()
{
 int i,j,rows,col;
 printf("Enter number of rows\n");
 scanf("%d",&amp;rows);
  printf("Enter number of columns\n");
 scanf("%d",&amp;col);

 int a1[rows][col],a2[rows][col],sub[rows][col];
 //Taking input for 1st matrix
 printf("Enter Matrix 1\n");
 for(i=0;i&lt;rows;i++)
 {
  for(j=0;j&lt;col;j++)
  {
   scanf("%d",(*(a1+i)+j));
  }
 }
  //Taking input for 2nd matrix
  printf("Enter Matrix 2\n");
 for(i=0;i&lt;rows;i++)
 {
  for(j=0;j&lt;col;j++)
  {
   scanf("%d",(*(a2+i)+j));
  }
 }
 //Subtraction of matrix
 for(i=0;i&lt;rows;i++)
 {
  for(j=0;j&lt;col;j++)
  {
   *(*(sub+i)+j)=*(*(a1+i)+j)-*(*(a2+i)+j);
  }
 }

 printf("Difference of above matrices(Matrix 1- Matrix 2) is\n");

  for(i=0;i&lt;rows;i++)
 {
  for(j=0;j&lt;col;j++)
  {
   printf("%d\t",*(*(sub+i)+j));
  }

  printf("\n");
 }

}

•Output 

Enter number of rows
2
Enter number of columns
2
Enter Matrix 1
60
70
23
56
Enter Matrix 2
30
35
67
100
Difference of above matrices(Matrix 1- Matrix 2) is
30      35
-44     -44

    

Comments

Popular posts from this blog

Java vs C++

STRUCTURE

SWITCH PROGRAMME