Pointer is that variable which stores the memory address of another variable. It means pointer is itself a variable which stores hex-decimal address of the location.
Suppose we assign the value 10 to any variable called num and this will store at any location in the memory then address of that location is stored by the pointer variable.
Pointer variable is declared by using format:
Data_type *variable;
int *pt;
To assign address to the pointer format:
pt= #
Here *pt will give the content of that location but pt will give address of that location. Similarly, pointer on floating, double and character data can be assigned and the pointer is always defined under that type to which the content is defined.
/*Program to show the use of pointers*/
#include<stdio.h>#include<conio.h>
void main()
{
int n,*p;
printf("\n enter any number");
scanf("%d",&n);
p=&n;
printf("\n number entered by the user is %d",n);
printf("\n address where number is stored is %d",&n);
printf("\n number entered is %d",*p);
printf("\n address is %d",p);
getch();
}
/*Program to show multiplication of two number’s with the help of pointers*/
#include<stdio.h>#include<conio.h>
void mul(int *p1,int *p2)
{
int mul;
mul=*p1* *p2;
printf("\n multiplication of two numbers is %d",mul);
}
void main()
{
int a,b;
printf("\n enter any value of a and b");
scanf("%d%d",&a,&b);
mul(&a,&b);
getch();
}
/*Program to show various operations applied on the user’s entered number with the help of pointers*/
#include<stdio.h>#include<conio.h>
void process(int *p1,int *p2)
{
int add,mul,sub;
add=*p1+*p2;
sub=*p1-*p2;
mul=*p1* *p2;
printf("\n addition is %d",add);
printf("\n subtraction is %d",sub);
printf("\n multiplication is %d",mul);
}
void main()
{
int a,b;
printf("\n enter any value of a and b");
scanf("%d%d",&a,&b);
process(&a,&b);
getch();
}
/*Program to show sum of two numbers with the help of pointers and return type statements */
#include<stdio.h>#include<conio.h>
int add(int *p1, int *p2)
{
int sum;
sum=*p1 + *p2;
return sum;
}
void main()
{
int a,b,c;
printf("\n enter any value of a and b");
scanf("%d%d",&a,&b);
c=add(&a,&b);
printf("\n addition of two numbers is %d",c);
getch();
}
No comments:
Post a Comment