Pointer program Examples in C Language



Content



1. Show Address of Variable using pointer

#include<stdio.h>

#include<conio.h>

void main()
{
int a=7;
float b=9.4;
char c='a';
clrscr();
printf("\n Address of A : %u",&a);
printf("\n Address of B : %u",&b);
printf("\n Address of C : %u",&c);
getch();
}

output:

Address of A : 3642140604
Address of B : 3642140600
Address of C : 3642140599




2. Show Actual value and Address of Actual value

#include<stdio.h>
#include<conio.h>
void main()
{
int a=15;
int *p;
clrscr();
p=&a;
printf("Address of a : %u",&a);
printf("\nAddress of p : %u",p);
printf("\nValue of a : %d",a);
printf("\nValue of *p : %u",*p);
getch();
}

output:

Address of a : 4122985812
Address of p : 4122985812
Value of a : 15
Value of *p : 15




3. Swapping using pointer

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int *x,*y,t;
clrscr();
printf("Enter the value:");
scanf("%d%d",&a,&b);
printf("\n\n");
x=&a;
y=&b;
t=*y;
*y=*x;
*x=t;
printf("x:%d\nY:%d",*x,*y);
getch();
}

output:

Enter the value:100   30

x:30
Y:100




4. Pass by Value in Pointer Example

#include <stdio.h>
#include <conio.h>

void square( int  );

int main ()
{
int a = 50;
clrscr();

square( a );

printf( "\n a = %d", a );

getch();
return 0;
}

void square( int  x )
{
x = x * x;
printf( "\n x = %d", x );
}

output:

x = 2500
a = 50



5. Pass by Reference in Pointer Example

#include <stdio.h>
#include <conio.h>

void square( int* );

int main ()
{
int a = 10;
clrscr();

square( &a );

printf( "\n a = %d", a );

getch();
return 0;
}

void square( int *pa )
{
*pa = *pa * *pa;
printf( "\n *pa = %d", *pa );
}

Output:

*pa = 100
a = 100




6. Returning by Reference in Pointer Example

#include <stdio.h>
#include <conio.h>

int* max( int*, int* );

int main ()
{
int a = 45, b = 42, *p;
clrscr();

p = max( &a, &b );

printf( "\n Maximum = %d", *p );

getch();
return 0;
}

int* max( int *pa, int *pb )
{
if( *pa > *pb )
return pa;
else
return pb;
}

output:

Maximum = 45




7. Addition operation on Pointer

#include<stdio.h>
#include<conio.h>
void main()
{
int p;
int *ptr;
clrscr();
ptr=&p;
printf("enter value:");
scanf("%d",ptr);
*ptr=*ptr+5;
printf("\n value of p is:%d",p);
printf("\n value of *ptr :%d",*ptr);
getch();
}

output:

enter value:5

value of p is:10
value of *ptr :10





8. Store sum of any two number in address of another variable

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
int *p1,*p2,*p3;
clrscr();
p1=&a;
p2=&b;
printf("Enter Two value:");
scanf("%d%d",p1,p2);
p3=&c;
*p3=*p1+*p2;
printf("\n Sum of two no is:%d",*p3);
getch();
}

Output:

Enter Tow Value:85 52
Sum of two no is:137





9. Arithmatic operation on Pointer

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*p1,*p2,x,y,z;
clrscr();
a=30,b=100;
p1=&a;
p2=&b;
x=*p1**p2-6;
y=4*-*p2;
printf("a=%d\tb=%d\tx=%d\ty=%d\n",a,b,x,y);
*p1=*p1+20;
*p2=*p2-10;
z=*p1**p2-6;
printf("a=%d\tb=%d\tz=%d\n",a,b,z);
getch();
}

output:

a=30    b=100   x=2994  y=-400
a=50    b=90    z=4494





10. Calculate length of string using pointer

#include<stdio.h>
void main()
{
char str1[20],*p1;
int i,len=0;
printf("Enter string: ");
scanf("%s",&str1);
p1=&str1;
while(*p1!='\0')
{
p1++;
len++;
}
printf("Length of String : %d",len);

}

output:

Enter string: Programming24
Length of String : 13




11. compare string using pointer

#include<stdio.h>
void main()
{
char str1[20],str2[20],*p1,*p2;
int i,flag=0;
printf("Enter first string: ");
scanf("%s",&str1);
printf("Enter second string: ");
scanf("%s",&str2);
p1=&str1;
p2=&str2;
while(*p1!='\0' || *p2!='\0')
{
if(*p1==*p2)
{
++p1;
++p2;
}
else
{
flag=1;
break;
}
}

if(*p1=='\0' && *p2=='\0' && flag==0)//first two conditions in if are for
                                                                    //matching the length of the strings
          printf("\nString are same");
        else
        printf("\nString are not same");

}

Output:

Enter first string: C
Enter second string: C++

String are not same





12. Reading array using pointer and print it using pointer

#include<stdio.h>
int main(){
   int  i;
   int arr[10];
   int *ptr;
   ptr=arr;
   printf("Enter 5 Elements: ");
   for(i=0;i<5;i++)
   {
     scanf("%d",ptr);
ptr++;
   }
   ptr=arr;

    printf("Entered Numbers:\n");
    for(i=0;i<5;i++)
   {
     printf("%d\n",*ptr);
ptr++;
   }
}

output:

Enter 5 Elements: 4 5 2 6 1
Entered Numbers:
4
5
2
6
1





13. Pointer to Structure as argument Write a program to read information of a book and display the information.

#include<stdio.h>
struct book
{
int bno;
char name[40];
};
void display(struct book*);
void main()
{
struct book b;
printf("\nEnter book no: ");
scanf("%d",&b.bno);
printf("Enter book name: ");
scanf("%s",b.name);
display(&b);
}
void display(struct book *b)
{
printf("\nDetails are: ");
printf("Book number= %d \n",b->bno);
printf("Book name= %s",b->name);

}

Output:

Enter book no: 23
Enter book name: CExamples

Details are:
Book number=23
Book name=CExamples




14. Pointer to Pointer or multiple pointer

#include<stdio.h>
void main()
{
   int a=5,*ptr3=&a,**ptr2=&ptr3,***ptr1=&ptr2;
   printf("%d",ptr1[0][0][0]);

}

Output: 5




15. Chain of Pointers

#include <stdio.h>
#include <conio.h>

int main ()
{
int a = 15;
int *p, **pp, ***ppp, ****pppp;
clrscr();

p = &a;

printf( "\n p = %u", p );
printf( "\n *p = %d", *p );

pp = &p;

printf( "\n\n pp = %u", pp );
printf( "\n *pp = %u", *pp );
printf( "\n **pp = %d", **pp);

ppp = &pp;

printf( "\n\n ppp = %u", ppp );
printf( "\n *ppp = %u", *ppp );
printf( "\n **ppp = %u", **ppp );
printf( "\n ***ppp = %d", ***ppp );

pppp = &ppp;

printf( "\n\n pppp = %u", pppp );
printf( "\n *pppp = %u", *pppp );
printf( "\n **pppp = %u", **pppp );
printf( "\n ***pppp = %u", ***pppp );
printf( "\n ****pppp = %d", ****pppp );

getch();
return 0;
}

Output:

p = 3438553460
*p = 15

pp = 3438553448
*pp = 3438553460
**pp = 15

ppp = 3438553440
*ppp = 3438553448
**ppp = 3438553460
***ppp = 15

pppp = 3438553432
*pppp = 3438553440
**pppp = 3438553448
***pppp = 3438553460
****pppp = 15

   


Reactions

Post a Comment

0 Comments