WhatsApp Group Join Now
Telegram Join Join Now

C : Pointer – Examples computer programming in hindi , pointer example very important in c language

pointer example very important in c language , C : Pointer – Examples computer programming in hindi :
Pointer एक variable है जो किसी दुसरे variable के address को hold करता है | Pointer variable एक valid memory address को contain करता है |
Pointer के benefits
1.Pointer variable से array और structure को efficiently handel कर सकते है |
2.Pointer variable का use , किसी function से multiple values को return करने के लिए किया जाता है |
3.Pointer variable का use , dynamic memory allocation के लिए किया जाता है |
4.Pointer variable का function मे argument pass करने के लिए किया जाता है |
किसी प्रोग्रामर के लिए pointer के concept को समजना मुश्किल होता है इसलिए इस article मे pointer के based पर कुछ उदहारण को discuss करेगे |
Example 1
write a basic program to understand operation using pointer.
इस उदहारण मे , pointer पर आधारित एक basic प्रोग्राम को discuss करगे |
Explanation
1.सबसे पहले variable को declare करेगे |जैसे a का type integer है |
2.इसके बाद pointer variable को declare करेगे |
4,यूजर से data को input करा लेते है |
5.p=&a से a का address को pointer मे store करा देते है |
6.*p से pointer variable की value को access किया जाता है |
code :
#include<stdio.h>
#include<conio.h>
void main()
{
int a ;
int *p;
printf(“Enter data :”);
scnaf(“%d”,&a);
p=&a;
printf(“Data of a= %d “,a);
printf(“Address of a = %u “,&a);
printf(“Value of pointer variable = %d “, *p);
printf(“Address of pointer variable = %u “,&p);
getch();
}
output :
Enter data :12
Data of a= 12
Address of a = 1200
Value of pointer variable = 1200
Address of pointer variable = 1202
Example 2
write a program to swapping operation using pointer.
इस उदहारण मे , pointer पर आधारित swapping के प्रोग्राम को discuss करगे |
Explanation
1.सबसे पहले दो variable को declare करेगे |
2.यूजर से data को input करा लेते है |
3.swapping function मे, दोनों variable को pass करते है |
4.उसके बाद दोनों variables को print कर देते है |
swapping function मे ,
1.सबसे पहले दो pointer variable मे , pass variables के address को assign करते है |
2. उसके बाद एक और variable को declare करते है |
3.फिर दोनों pointer variable के value ( यानि address ) interchange करते है |
code :
#include<stdio.h>
#include<conio.h>
void swap(int * . int *);
void main()
{
int a ,b;
printf(“Enter data :”);
scnaf(“%d”,&a);
printf(“Enter data :”);
scnaf(“%d”,&b);
printf(“Data of a= %d “,a);
printf(“Data of b= %d “,b);
swap(&a,&b);
printf (“After Swapping”);
printf(“Data of a= %d “,a);
printf(“Data of b= %d “,b);
getch();
}
void swap(int *a, int *b)
{
int t;
t=*a;
*a=*b;
*b=*a;
}
output :
Enter data :12
Enter data :43
Data of a= 12
Data of b=43
After Swapping
Data of a=43
Data of b=12
Example 3
write a program to copy an array into another array using pointer.
इस उदहारण मे , किसी array को दुसरे array मे copy करने का प्रोग्राम लिखा जाता है |
Explanation
1.सबसे पहले array की size को input करा लेते है जिसे size variable मे assign करा लेते है |
2.उसके बाद दो array को declare कर देते है |
3.उसके बाद दो pointer variables को declare कर देते है जिसमे से एक variable copy array के लिए और दुसरा variable , copied array को hold करता है |
4.एक और pointer variable को declare करते है जो की source array के size-1 के address को hold करता है |
5.इसके बाद source array print करा देते है |
6.loop को चलाया जाता है जिससे source array को copy array मे copy किया जाता है |
7.फिर source array और copy array को print किया जाता है |
code :
#include<stdio.h>
#include<conio.h>
void main()
{
int a [size] ,b[size]; // a[] = source array or b[] = copy array //
int size,i;
int *s=a;
int *c=b;
int *end;
printf(“Enter size :”);
scnaf(“%d”,&size);
printf(“Enter data :”);
for(i=0;i<size;i++)
{
scnaf(“%d”,(s+i));
}
end=&a[size-1];
printf (“Source array is :”)
for(i=0;i<size;i++)
{
printf (“%d \t”,*a);
a++;
}
while (s<=e)
{
*b=*a;
a++;
b++;
}
printf (“Source Array : “);
for(i=0;i<size;i++)
{
printf (“%d \t”,*a);
a++;
}
printf (“Copy Array : “);
for(i=0;i<size;i++)
{
printf (“%d \t”,*b);
b++;
}
getch();
}output
Enter size : 4
Enter data : 12 23 43 56
Source array is : 12 23 43 56
Source Array : 12 23 43 56
Copy Array : 12 23 43 56

Example 4
write a program to compare two strings using pointer.
इस उदहारण मे , pointer से दो strings को compare करने का प्रोग्राम लिखा जाता है |
Explanation
1.सबसे पहले string की size को macro statement से set कर लेते है |
2.उसके बाद दो string को declare कर देते है|
3.दोनों strings को input करा देते है |
4. इन दोनों strings को compare function मे pass करते है |
5.compare function के return value पर आउटपुट print होता है |compare function मे ,
1.सबसे पहले दो pointer variables मे string की values को assign कर देते है |
2.loop चलाया जाता है जिसमे दोनों string को character wise compare किया जाता है |
3.उसके बाद दोनों string pointer के difference को return करा देते है |
code :
#include<stdio.h>
#include<conio.h>
#define size 20
void main()
{
char a [size] ,b[size];
printf(“Enter first string :”);
gets(a);
printf(“Enter Second string :”);
gets(b);
int count = compare(a,b);
if(count ==0)
{
printf (“Both String are same .”)
}
else if(count <0)
{
printf (“String ‘a’ is large than String ‘b’.”);
}
else
{
printf (“String ‘b’ is large than String ‘a’.”);
}
getch();
}
int compare (int *s,int *c)
{
while ((*s && *c) && (*s==*c))
{
s++;
c++;
}
return *s-*c;
}
output
Enter first string : parth
Enter Second string :part
String ‘b’ is large than String ‘a’.
Example 5
write a program to deal with two dimensional array using pointer.
इस उदहारण मे , pointer से two dimensional array को access करने का प्रोग्राम लिखा जाता है |
Explanation
1.सबसे पहले two dimensional array को declare किया जाता है |
2.उसके बाद एक pointer variable ‘m’ को declare किया जाता है |
3.अगर किसी two dimensional array के position (0,1) को access करना होता है तब pointer variable ((m+0)+1) की value को एक्सेस करेगे |
4.यूजर से two dimensional array के elements को input करा लेते है |
5.उसके बाद two level looping की जाती है जिससे two dimensional array को स्कैन किया जाता है |
6.इसके बाद दुसरे looping मे , two dimensional array के element को matrix form मे print करा देते है |
code :
#include<stdio.h>
#include<conio.h>
#define size 3
void main()
{
int a [size][size];
int *m= &a;
printf(“Enter Element :”);
for (i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
scanf(“%d”,(*(m+i)+j));
}
}
printf(“Matrix Form \n”);
for (i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
scanf(“%d”,(*(m+i)+j));
}
printf(“\n”);
}
getch();
}
output
Enter Element : 12 23 34 54 65 67 89 32 55
Matrix Form
12 23 34
54 65 67
89 32 55 Example 6
write a program to add two array using pointer.
इस उदहारण मे , pointer से do array को add करने का प्रोग्राम लिखा जाता है |
Explanation
1.सबसे पहले two dimensional array को declare किया जाता है |
2.उसके बाद एक pointer variable ‘m’ को declare किया जाता है |
3.उसके बाद two dimensional array को declare किया जाता है |
4..उसके बाद एक pointer variable ‘n’ को declare किया जाता है |
5.सबसे last मे , एक और array को declare किया जाता है जो की दोनों array के addition को hold करता है |
6.अगर किसी two dimensional array के position (0,1) को access करना होता है तब pointer variable ((m+0)+1) की value को एक्सेस करेगे |
7.यूजर से दोनों two dimensional array के elements को input करा लेते है |
8.उसके बाद two level looping की जाती है जिससे two dimensional array को स्कैन किया जाता है |
9.इसके बाद दुसरे looping मे , दोनों array के elements को add किया जाता है |और third array मे store किया जाता है |
10.third array को print किया जाता है
code :
#include<stdio.h>
#include<conio.h>
#define size 3
void main()
{
int a [size][size];
int b[size][size];
int c[size][size];
int *m= &a;
int *n= &b;
int *o=&c;
printf(“Enter Element :”);
for (i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
scanf(“%d”,(*(m+i)+j));
}
}
printf(“Enter Element :”);
for (i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
scanf(“%d”,(*(n+i)+j));
}
}
for (i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
*(c+i)+j=*(n+i)+j+*(m+i)+j;
}
}
printf(“Matrix Form of addition \n”);
for (i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
scanf(“%d”,(*(c+i)+j));
}
printf(“\n”);
}
getch();
}
output
Enter Element : 12 23 34 54 65 67 89 32 55
Enter Element : 12 20 36 80 65 68 80 89 65
Matrix Form of addition
24 43 70
134 130 135
169 121 130Example 7
write a program to multiply two array using pointer.
इस उदहारण मे , pointer से दो array को multiply करने का प्रोग्राम लिखा जाता है |
Explanation
1.सबसे पहले two dimensional array को declare किया जाता है |
2.उसके बाद एक pointer variable ‘m’ को declare किया जाता है |
3.उसके बाद two dimensional array को declare किया जाता है |
4..उसके बाद एक pointer variable ‘n’ को declare किया जाता है |
5.सबसे last मे , एक और array को declare किया जाता है जो की दोनों array के multiply को hold करता है |
6.अगर किसी two dimensional array के position (0,1) को access करना होता है तब pointer variable ((m+0)+1) की value को एक्सेस करेगे |
7.यूजर से दोनों two dimensional array के elements को input करा लेते है |
8.उसके बाद two level looping की जाती है जिससे two dimensional array को स्कैन किया जाता है |
9.इसके बाद दुसरे looping मे , दोनों array के elements को multiply किया जाता है |और third array मे store किया जाता है |
10.third array को print किया जाता है
code :
#include<stdio.h>
#include<conio.h>
#define size 3
void main()
{
int a [size][size];
int b[size][size];
int c[size][size];
int *m= &a;
int *n= &b;
int *o=&c;
printf(“Enter Element :”);
for (i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
scanf(“%d”,(*(m+i)+j));
}
}
printf(“Enter Element :”);
for (i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
scanf(“%d”,(*(n+i)+j));
}
}
for (i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
*(c+i)+j=*(n+i)+j * *(m+i)+j;
}
}
printf(“Matrix Form of Multiplication \n”);
for (i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
scanf(“%d”,(*(c+i)+j));
}
printf(“\n”);
}
getch();
}
output
Enter Element : 1 2 3 5 6 6 8 3 5
Enter Element : 12 20 36 80 65 68 80 89 65
Matrix Form of Multiplication
24 40 108
400 390 408
640 267 325

Example 8

write a program to calculate length of string using pointer.
इस उदहारण मे , pointer से किसी string के length को claculate करने का प्रोग्राम लिखा जाता है |
Explanation
1.सबसे पहले string को use से input किया जाता है |
2.loop चलाया जाता है ये loop तब तक चलता है जब तक की string pointer की value null नहीं हो जाती है |
3.loop की body मे count को increment किया जाता है |
4.count की value ही string की length होती है |
code :
#include<stdio.h>
#include<conio.h>
#define size 20
void main()
{
char a[size];
printf(“Enter String : “);
gets(a);
char *m=a;
int count =0;
while (*m != ‘/0’)
{
count ++;
m++;
}
printf (“Length of string : %d “, count );
getch();
}
output
Enter String : Parth
Length of string : 5