Programs Using Pointer


Write a Simple Program using pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int *ptr;
clrscr();
ptr=&a;
printf("Enter Value of a:");
scanf("%d",&a);
printf("Address of a:%u\nValue of a:%d",ptr,*ptr);
getch();
}
Output:
Enter Value of a:10
Address of a:65524
Value of a:10

Write a Program to display variable using different methods.

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int *ptr;
clrscr();
ptr=&a;
printf("Enter Value of a:");
scanf("%d",&a);
printf("Value of a:%d\n",a);
printf("Value of a:%d\n",*ptr);
printf("Value of a:%d\n",*(&a));
getch();
}
Output:
Enter Value of a:10
Value of a:10
Value of a:10
Value of a:10

     
Write a Program using void pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
float b;
char ch;
void *ptr;
clrscr();
printf("Enter Value of a:");
scanf("%d",&a);
printf("Enter Value of b:");
scanf("%f",&b);
printf("Enter Value of ch:");
scanf("%s",&ch);
ptr=&a;
printf("Value of a is :%d\n",*(int*)ptr);
ptr=&b;
printf("Value of b:%f\n",*(float*)ptr);
ptr=&ch;
printf("Value of ch:%c\n",*(char*)ptr);
getch();
}
Output:
Enter Value of a:10
Enter Value of b:10.50
Enter Value of ch:x
Value of a:10
Value of b:10.500000
Value of ch:x
Value of a:10

Write a Program to show use of Pointer of Pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int *ptr;
int **dptr;
clrscr();
printf("Enter Value of a:");
scanf("%d",&a);
ptr=&a;
dptr=&ptr;
printf("Value of a:%d Address of a:%u\n",a,ptr);
printf("Value of a:%d Address of ptr:%u\n",*ptr,dptr);
printf("Value of a:%d Address of dptr:%u\n",**dptr,&dptr);
getch();
}
Output:
Enter Value of a:10
Value of a:10 Address of a: 65524
Value of a:10 Address of ptr: 65522
Value of a:10 Address of dptr: 65520

Write a Program to display element of array using pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
int *ptr;
clrscr();
printf("Enter Elements of Array:\n");
for(i=0;i<5;i++)
{
printf("Enter Value of a[%d]:",i);
scanf("%d",&a[i]);
}
ptr=&a[0];
for(i=0;i<5;i++)
{
printf("Value of a[%d]:%d Address is %u\n",i,*ptr,ptr);
ptr++;
}
getch();
}
Output:
Enter Elements of Array:
Enter Value of a[0]:10
Enter Value of a[1]:20
Enter Value of a[2]:30
Enter Value of a[3]:40
Enter Value of a[4]:50
Value of a[0]:10 Address is: 65516
Value of a[1]:10 Address is: 65518
Value of a[2]:10 Address is: 65520
Value of a[3]:10 Address is: 65522
Value of a[4]:10 Address is: 65524

Write a Program to find sum of array element using pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,sum=0;
int *ptr;
clrscr();
printf("Enter Elements of Array:\n");
for(i=0;i<5;i++)
{
printf("Enter Value of a[%d]:",i);
scanf("%d",&a[i]);
}
ptr=&a[0];
for(i=0;i<5;i++)
{
sum=sum + *ptr;
ptr++;
}
printf("Sum=%d",sum);
getch();
}
Output:
Enter Elements of Array:
Enter Value of a[0]:10
Enter Value of a[1]:20
Enter Value of a[2]:30
Enter Value of a[3]:40
Enter Value of a[4]:50
Sum=150

Write a Program to find maximum number from array using Pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,max;
int *ptr;
clrscr();
printf("Enter Elements of Array:\n");
for(i=0;i<5;i++)
{
printf("Enter Value of a[%d]:",i);
scanf("%d",&a[i]);
}
ptr=&a[0];
max=a[0];
for(i=0;i<5;i++)
{
if(*ptr>max)
max=*ptr;
ptr++;
}
printf("Max=%d",max);
getch();
}
Output:

Enter Elements of Array:
Enter Value of a[0]:10
Enter Value of a[1]:5
Enter Value of a[2]:23
Enter Value of a[3]:45
Enter Value of a[4]:29
Max=45

Download Projects


Download Programs