pointer example in computer programming c पॉइंटर उदाहरण , some pointer examples in c language in hindi :
Pointer एक variable है जो किसी दुसरे variable के address को hold करता है | Pointer variable एक valid memory address को contain करता है |
Pointer के benefits
1.Pointer variable से array और structure को efficiently handle कर सकते है |
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 पर कुछ advance उदहारण को discuss करेगे |
Example 1
write a program to sorting operation (Ascending order) in array using pointer.
इस उदहारण मे , pointer से किसी array मे sorting operation के प्रोग्राम को discuss करगे |
Explanation
सबसे पहले header field मे , चार यूजर define functions को declare करेगे |
main function:
1.यूजर से array की size और elements को input करा लेते है |उसके बाद scanarray() function को call करते है जिसमे declare array name और size pass होती है |
2.उसके बाद sort() function को call किया जाता है , जिसमे declare array name , size , compare function [ascend()] pass होती है |
3.फिर print () function से array के element को printकरा देते है |
scanarray() मे ,
इस function से , pointer से array मे यूजर द्वारा input की गयी values को assign किया जाता है |
1.सबसे पहले pointer variable end को set किया जता है |
2.जब तक pointer variable ‘a’ की value end से छोटी या equal होती है तब तक array मे value assign होती है |
ascend() मे ,
इस function मे array के दो elements मे compare किया जाता है |अगर swapping compare function ascend() की value पर निर्भर करता है |
अगर इस function से ‘0’ से ज्यादा value आती है तब swapping function होता है |
print() मे
इस function से , array के elements को print किया जाता है |
1.सबसे पहले pointer variable end को set किया जता है |
2.जब तक pointer variable ‘a’ की value end से छोटी या equal होती है तब तक array मे value print होती है |
sort() मे ,
इस function array को sort operation का code होता है |इसमें दो pointer variables current और check होते है|
1.सबसे पहले end को set किया जाता है |
2.loop चलाया जाता है loop तब तक चलता है जब pointer variable current की value end से छोटी होती है |
3.इस loop की body मे ,pointer variable को current से initial करते है | उसके बाद एक और loop चलाया जाता है |
4.इस loop की body मे compare function को call किया जाता है जिसमे current और check को pass किया जाता है |
5.अगर compare function की value ‘0’ से ज्यादा होती तो swapping operation perform होता है |
6.current और check को increment कर देते है |
source code :
#include
#include
void scanarray(int *a,int size);
void print(int *a, int size);
int ascend (int *num1,int *num2);
int sort (int *a,int size,int(*compare)(int * ,int *));
void main()
{
int array[size],size;
printf (“Enter Size”);
scanf(“%d”,&size);
printf(“Enter Elements”);
scanarray(array , size);
printf(“Before Sorting”);
print(array , size);
printf(“After sorting”);
sort (array,size,ascend);
print(array,size);
getch();
}
void scanarray(int *a,int size)
{
int *end = (a+size-1);
while(a<=end)
{
scnaf(“%d”,a++);
}
}
void print(int *a, int size)
{
int *end = (a+size-1);
while(a<=end)
{
printf(“%d”,*(a++));
}
}
int ascend (int *num1,int *num2)
{
return (*num1)-(*num2);
}
void sort (int *a,int size,int(*compare)(int * ,int *))
{
int *end = (a+size-1);
int *current=a;
int *check;
while(current <= end)
{
check= current;
while(check <= end ) { if(compare (current, check )>0)
{
*current ^= *check;
*check ^= *current;
*current ^= *check;
}
check++;
}
current++;
}
}
Example 2
write a program to sorting operation (descending order) in array using pointer.
इस उदहारण मे , pointer से किसी array मे sorting operation के प्रोग्राम को discuss करगे |
Explanation
सबसे पहले header field मे , चार यूजर define functions को declare करेगे |
main function:
1.यूजर से array की size और elements को input करा लेते है |उसके बाद scanarray() function को call करते है जिसमे declare array name और size pass होती है |
2.उसके बाद sort() function को call किया जाता है , जिसमे declare array name , size , compare function [descend()] pass होती है |
3.फिर print () function से array के element को printकरा देते है |
scanarray() मे ,
इस function से , pointer से array मे यूजर द्वारा input की गयी values को assign किया जाता है |
1.सबसे पहले pointer variable end को set किया जता है |
2.जब तक pointer variable ‘a’ की value end से छोटी या equal होती है तब तक array मे value assign होती है |
descend() मे ,
इस function मे array के दो elements मे compare किया जाता है |अगर swapping compare function ascend() की value पर निर्भर करता है |
अगर इस function से ‘0’ से ज्यादा value आती है तब swapping function होता है |
print() मे
इस function से , array के elements को print किया जाता है |
1.सबसे पहले pointer variable end को set किया जता है |
2.जब तक pointer variable ‘a’ की value end से छोटी या equal होती है तब तक array मे value print होती है |
sort() मे ,
इस function array को sort operation का code होता है |इसमें दो pointer variables current और check होते है|
1.सबसे पहले end को set किया जाता है |
2.loop चलाया जाता है loop तब तक चलता है जब pointer variable current की value end से छोटी होती है |
3.इस loop की body मे ,pointer variable को current से initial करते है | उसके बाद एक और loop चलाया जाता है |
4.इस loop की body मे compare function को call किया जाता है जिसमे current और check को pass किया जाता है |
5.अगर compare function की value ‘0’ से ज्यादा होती तो swapping operation perform होता है |
6.current और check को increment कर देते है |
source code :
#include
#include
void scanarray(int *a,int size);
void print(int *a, int size);
int descend (int *num1,int *num2);
int sort (int *a,int size,int(*compare)(int * ,int *));
void main()
{
int array[size],size;
printf (“Enter Size”);
scanf(“%d”,&size);
printf(“Enter Elements”);
scanarray(array , size);
printf(“Before Sorting”);
print(array , size);
printf(“After sorting”);
sort (array,size,descend);
print(array,size);
getch();
}
void scanarray(int *a,int size)
{
int *end = (a+size-1);
while(a<=end)
{
scnaf(“%d”,a++);
}
}
void print(int *a, int size)
{
int *end = (a+size-1);
while(a<=end)
{
printf(“%d”,*(a++));
}
}
int descend (int *num1,int *num2)
{
return (*num2)-(*num1);
}
void sort (int *a,int size,int(*compare)(int * ,int *))
{
int *end = (a+size-1);
int *current=a;
int *check;
while(current <= end)
{
check= current;
while(check <= end ) { if(compare (current, check )>0)
{
*current ^= *check;
*check ^= *current;
*current ^= *check;
}
check++;
}
current++;
}
}
Example 3
write a program to reverse operation in array using pointer.
इस उदहारण मे , pointer से किसी array मे reverse operation के प्रोग्राम को discuss करगे |
Explanation
सबसे पहले header field मे , दो user define functions को declare करेगे |
main function:
1.यूजर से array की size और elements को input करा लेते है |उसके बाद scanarray() function को call करते है जिसमे declare array name और size pass होती है |
2.इस प्रोग्राम मे दो pointer variables को use किया जाता है |जिसमे एक pointer variable left, left side के element के address को hold करता है और pointer variable right , right side के element के address को hold करता है |
5. loop चलाया जाता है :ये loop तब तक चलता है जब तक left और right की value छोटी होती है |
6.loop इस body मे , left और right variable की value को interchange किया जाता है |
3.फिर print () function से array के element को printकरा देते है |
scanarray() मे ,
इस function से , pointer से array मे यूजर द्वारा input की गयी values को assign किया जाता है |
1.सबसे पहले pointer variable end को set किया जता है |
2.जब तक pointer variable ‘a’ की value end से छोटी या equal होती है तब तक array मे value assign होती है |
print() मे
इस function से , array के elements को print किया जाता है |
1.सबसे पहले pointer variable end को set किया जता है |
2.जब तक pointer variable ‘a’ की value end से छोटी या equal होती है तब तक array मे value print होती है |
source code :
#include
#include
void scanarray(int *a,int size);
void print(int *a, int size);
void main()
{
int *left ,*right ;
int *temp;
int array[size],size;
printf (“Enter Size”);
scanf(“%d”,&size);
printf(“Enter Elements”);
scanarray(array , size);
printf(“Before reverse operation”);
print(array , size);
printf(“After reverse operation”);
left = array;
right = (array+size)-1;
while (left<right)
{
*temp=*left ;
*left=*right;
*right = *left ;
left++;
right–;
}
print(array,size);
getch();
}
void scanarray(int *a,int size)
{
int *end = (a+size-1);
while(a<=end)
{
scnaf(“%d”,a++);
}
}
void print(int *a, int size)
{
int *end = (a+size-1);
while(a<=end)
{
printf(“%d”,*(a++));
}
}
Example 4
write a program to searching operation in array using pointer.
इस उदहारण मे , pointer से किसी array मे searching operation के प्रोग्राम को discuss करगे |
Explanation
सबसे पहले header field मे , तीन user define functions को declare करेगे |
main function:
main function:
1.यूजर से array की size और elements को input करा लेते है |उसके बाद scanarray() function को call करते है जिसमे declare array name और size pass होती है |
2.उसके बाद जिस element को search करना है use यूजर से input करा लेते है |
3.उसके बाद searching() function को call किया जाता है , जिसमे declare array name , size , search element pass होती है |
4.search () के आधार पर प्राप्त आउटपुट को print करा लेते है |
scanarray() मे ,
इस function से , pointer से array मे यूजर द्वारा input की गयी values को assign किया जाता है |
1.सबसे पहले pointer variable end को set किया जता है |
2.जब तक pointer variable ‘a’ की value end से छोटी या equal होती है तब तक array मे value assign होती है |
print() मे
इस function से , array के elements को print किया जाता है |
1.सबसे पहले pointer variable end को set किया जता है |
2.जब तक pointer variable ‘a’ की value end से छोटी या equal होती है तब तक array मे value print होती है |
search()
1.सबसे पहले pointer variable end को set किया जता है |
2.फिर loop चलाया जाता है जिसमे search element को array के सभी element से check किया जाता है |
3.अगर search element array मे मिल जाता है तब उसका index return हो जाता है |
4.अगर search element नहीं मिलता है तब -1 return हो जाता है |
source code :
#include
#include
void scanarray(int *a,int size);
void print(int *a, int size);
int search(int *a ,int size , int search);
void main()
{
int s;
int *temp;
int array[size],size;
printf (“Enter Size”);
scanf(“%d”,&size);
printf (“Enter element that you want search”);
scanf(“%d”,&s);
printf(“Enter Elements”);
scanarray(array , size);
int count = search(array,size,s);
if(count == -1)
{
printf (“Enter element does not in array”);
}
else
{
printf(“%d is at %d in array.”,s,count);
getch();
}
void scanarray(int *a,int size)
{
int *end = (a+size-1);
while(a<=end)
{
scnaf(“%d”,a++);
}
}
void print(int *a, int size)
{
int *end = (a+size-1);
while(a<=end)
{
printf(“%d”,*(a++));
}
}
int search(int *a, int size , int search)
{
int *end = (a+size-1);
int index=0;
while (a<=end && *a != search)
{
a++;
index++;
}
if (index < end)
{
return index;
}
else
{
return -1;
}
}