SIMPLE eXAMPLE OF POINTER
#include<stdio.h>
#include<conio.h>
void main(){
int x=50;
clrscr();
int *p;
p=&x;//stores the address of number
variable
printf("Value of variable var is:
%d\n", x);
printf("Value of variable var
is: %d\n", *p);
printf("Address of variable var
is: %x\n", &x);
printf("Address of variable var
is: %x\n", p);
printf("Address of pointer p
is: %x", &p);
getch();
}
Output:
Value of variable var is: 50
Value of variable var is: 50
Address of variable var is: cfb7d734
Address of variable var is: cfb7d734
Address of pointer p is: cfb7d738
Add two numbers using pointers
#include <stdio.h>
#include<conio.h>
void main()
{
int firstNo, secondNo, *p, *q, sum;
clrscr();
printf("Enter two Number\n");
scanf("%d%d", &firstNo, &secondNo);
p = &firstNo;
q = &secondNo;
sum = *p + *q;
printf("Sum is = %d\n",sum);
getch();
}
Output:
Enter two Number
20
30
Sum is = 50
Add numbers using call by
reference
#include <stdio.h>
#include<conio.h>
int add(int *x, int *y); //function declaration
int add(int *x, int *y) //function definition
{
int sum;
sum = *x + *y;
return sum;
}
void main()
{
int firstNo, secondNo,sum;
int add(int *x, int *y);
printf("Input two
Number\n");
scanf("%d%d",
&firstNo, &secondNo);
sum = add(&firstNo,
&secondNo); //function
call
printf("sum is %d\n",sum);
getch();
}
Output:
Input two Number
45
67
sum is 112
Find the factorial of a given number
using pointers
#include <stdio.h>
#include<conio.h>
void findFact(int n,int*fact1); //function
declaration
void findFact(int n,int *fact1) //function definition
{
int i;
*fact1
=1;
for(i=1;i<=n;i++)
*fact1=*fact1*i;
}
void main()
{
int
fact;
int
num1;
clrscr();
printf("
Input a number : ");
scanf("%d",&num1);
findFact(num1,&fact); //function call
printf(" The Factorial of %d is : %d \n",num1,fact);
getch();
}
Output:
Input a number : 5
The Factorial of 5 is : 120
Swap elements using call by reference in pointer
#include <stdio.h>
#include<conio.h>
void swapNumbers(int *x,int *y);
void main()
{
int s1,s2;
clrscr();
printf(" Input the value of 1st element : ");
scanf("%d",&s1);
printf("
Input the value of 2nd element : ");
scanf("%d",&s2);
printf("The value before swapping are :\n");
printf(" element 1 = %d\n element 2 = %d\n ",s1,s2);
swapNumbers(&s1,&s2);
printf(" The value after swapping are :\n");
printf(" element 1 = %d\n element 2 = %d\n ",s1,s2);
getch();
}
void swapNumbers(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
Output:
Input the value of 1st element : 34
Input the value of 2nd element : 56
The value before swapping are :
element 1 = 34
element 2 = 56
The value after swapping are :
element 1 = 56
element 2 = 34
To Check whether a number is Larger or Not using pointer.
#include <stdio.h>
#include<conio.h>
void *findLarg(int *x, int*y);
void main()
{
int num1,num2;
printf(" Input the first number : ");
scanf("%d", &num1);
printf(" Input the second
number : ");
scanf("%d", &num2);
findLarg(&num1, &num2);
getch();
}
void *findLarg(int *n1, int *n2)
{
if(*n1 > *n2)
{
printf("larger number is %d\n",*n1);
}
else{
printf("larger number is %d\n",*n2);
}
}
Output:
Input the first number : 12
Input the second number : 23
larger number is 23
To print the elements of an array in
reverse order
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i, arr1[20];
int *pt;
printf(" Enter the number of elements\n");
scanf("%d",&n);
pt = &arr1[0];
for(i=0;i<n;i++)
{
printf(" element is %d : ",i+1);
scanf("%d",pt);
pt++;
}
pt
= &arr1[n - 1];
printf("\n The elements of array in reverse
order are :\n");
for (i = n; i > 0; i--)
{
printf(" element is
%d\n", *pt);
pt--;
}
getch();
}
Output:
Enter the number of elements
3
element is 1 : 23
element is 2 : 9
element is 3 : 67
The elements of array in reverse order are :
element is 67
element is 9
element is 23
To compute the
sum of all elements in an array using pointers.
#include <stdio.h>
#include<conio.h>
void main()
{
int arr1[20];
int i,n, sum = 0;
int *pt;
clrscr();
printf("
Enter the number of elements\n");
scanf("%d",&n);
printf(" Input %d number of elements in the array : \n",n);
for(i=0;i<n;i++)
{
printf(" element - %d : ",i+1);
scanf("%d",&arr1[i]);
}
pt = arr1;
for
(i = 0; i < n; i++) {
sum = sum + *pt;
pt++;
}
printf(" The sum of array is : %d\n", sum);
getch();
}
Output:
Enter the number of elements
3
Input 3 number of elements in the array :
element - 1 : 45
element - 2 : 67
element - 3 : 78
The sum of array is : 190
Write a program
in C to print a string in reverse using a pointer.
#include <stdio.h>
#include<conio.h>
void main()
{
char str1[30];
char revstring[30];
char *strptr = str1;
char *rvptr = revstring;
int i=-1;
clrscr();
printf(" Input a string : \n");
scanf("%s",str1);
while(*strptr)
{
strptr++;
i++;
}
while(i>=0)
{
strptr--;
*rvptr = *strptr;
rvptr++;
--i;
}
*rvptr='\0';
printf(" Reverse of the string is : %s\n\n",revstring);
getch();
}
Output:
Input a string :
LearningPoint92
Reverse of the string is : 29tnioPgninraeL
No comments:
Post a Comment