Pointers in C

We store our data in a variable but pointers store address of variable which stores our data.
Pointers in C
At the time of initialization value of pointer is null and its value is 0.
We can subtract two pointers to find out how many elements are available but we can’t add , multiply or divide pointers

Declaration of Pointers

int  *pot;  
Int * pot; 
Int* pot;
int * pot, i, j;   //  pot is a pointer variable but i , j are integer variable 
double  * dpot // double pointer 
A double pointer can hold address of a double variable only, it can’t hold address of an integer.

unary operators

& Address Of operator
* Value Of operator

#include <stdio.h>
int main(void){
   int i=105;
   int *pot; // Pointer of an integer //
   pot=&v;   // assign address of i to pot //
   printf(" pot=%d \n *pot= %d, \n i=%d", pot, *pot, i);
   return 0;
}

Assign data by using pointers

#include <stdio.h>
int main(void){
   int *pot,num=5;
   pot=#
   *pot=100;
      printf("num=%d ",num);
}
Output
num=100

Understanding difference between pointer and normal variable

#include <stdio.h>
int main(void){
   int i=3;
   int *pot; // Pointer of an integer //
   pot=&i;   // assign address of i to pot //
   printf(" pot=%d \n *pot= %d, \n i=%d", pot, *pot,i);
   return 0;
}
Output is here
pot=6356744
 *pot= 3,
 i=3
#include <stdio.h>
#include <string.h>
int main(void){
   int u=3;
   int v;
   int *pu; // Pointer of an integer //
   int *pv; // POinter of an integer //

   pu=&u;   // assign address of u to pv //
   v=*pu;    // assign value of u to v //
   pv= &v; //  assign address of v to pv //

   printf("\n u=%d &u=%X  pu=%X *pu=%d", u,&u,pu,*pu);
   printf("\n\n v=%d &v=%X pv=%X *pv=%d",v,&v,pv,*pv);
   return 0;
}
Output is here
 u=3 &u=60FF04  pu=60FF04 *pu=3

 v=3 &v=60FF00 pv=60FF00 *pv=3

Add two numbers by using pointers

#include <stdio.h>
int main(void){
   int *pot1,*pot2,sum, num1,num2;
 num1=100;
 num2=200;
 pot1=&num1;
 pot2=&num2;

 sum= *pot1 + * pot2;
      printf("sum =%d ",sum);
 return 0;
}
Output is
sum =300

Swap two numbers by using pointers

#include <stdio.h>
int main(void){
   int num1=5,num2=10,temp=0;
   temp=num2;
   int *pot1, *pot2;

   pot1=&num1;
   pot2=&num2;

   temp=*pot1;
   *pot1=*pot2;
   *pot2=temp; // This is same as num2=temp;

   printf("num1=%d , num2=%d",num1,num2);
   }
Output is here
num1=10 , num2=5

Instead of variable pass pointer as parameter to a function.

Use one function to return final salary after an increase of 15%
#include <stdio.h>
int salary(int *basic){
    int final_sal=0;
    final_sal=*basic*1.5;
    return final_sal;
}
int main(void){
   int *pot, sal,final_sal;

   sal=1000;
   pot=&sal;
   final_sal=salary(pot);
      printf("Final Salary =%d ",final_sal);
}
Output is here
Final Salary =1500

Pointer to an array

#include <stdio.h>
int main(void){
   int i,num[5]={3,4,5,6,7};
   int *pot; // Pointer of an integer //
   pot=#   // assign address of num to pot //

   for(i=0;i<5;i++){
    printf("value :  %d : ", num[i]);
    printf("address : %d : ", pot);
    printf("\n");
    pot++;
   }

}
Output is here
value :  3 : address : 6356724 :
value :  4 : address : 6356728 :
value :  5 : address : 6356732 :
value :  6 : address : 6356736 :
value :  7 : address : 6356740 :

Passing Pointer of an array as parameter to function

#include <stdio.h>
int array_sum(int *pot){
int k,sum=0;
for(k=0;k<5;k++){
   sum = sum+ *pot;
   pot++;
}
//sum=ar[2];
return sum;
}
int main(void){
int j[5]={1,2,3,4,5};
int sum;
int *pot;
pot=&j[0];
sum=array_sum(pot);
printf("Sum = %d \n",sum);
return 0;
}
Output is here
Sum = 15
Visitors Rating
Your Rating




Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer