C++ में Pointer Arithmetic
In this page:
pointer_name++; // move to the next element
pointer_name--; // move to the previous element
pointer_name + n;
एक Pointer Increment करना
ptr++ से एक pointer increment करना raw address value में बस 1 add नहीं करता — यह pointer को exactly उस type के size जितना, bytes में, आगे बढ़ाता है जिसकी ओर यह point करता है, इसलिए एक int* 4 bytes आगे move करता है जबकि एक double* 8 आगे move करता है।
यही exactly है जो ptr++ को array के अगले element पर सही तरीके से land कराता है, current वाले के बीच में नहीं।
उदाहरण: Incrementing a Pointer
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {10, 20, 30};
int *ptr = arr;
ptr++;
// Print to the console with cout
std::cout << *ptr << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Pointer Decrement करना
ptr-- से decrement करना reverse में same तरह काम करता है, pointer को एक single raw byte की बजाय एक पूरे element जितने bytes पीछे step कराता है, जो एक array में सही तरीके से backward चलने के लिए essential है।
उदाहरण: Decrementing a Pointer
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {10, 20, 30};
int *ptr = &arr[2];
ptr--;
// Print to the console with cout
std::cout << *ptr << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Integers जोड़ना और Subtract करना
एक integer n को सीधे जोड़ना या subtract करना, जैसे ptr + n, pointer को n raw bytes की बजाय एक साथ n पूरे elements आगे या पीछे move करता है — इसी तरह आप एक loop में एक-एक step increment करने की बजाय ptr + 4 से array के पांचवें element पर सीधे jump कर सकते हैं।
उदाहरण: Adding and Subtracting Integers
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {10, 20, 30, 40};
int *ptr = arr;
// Print to the console with cout
std::cout << *(ptr + 2) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
दो Pointers Subtract करना
same type के एक pointer को दूसरे से subtract करना, जैसे ptr2 - ptr1, आपको उनके बीच अलग करने वाले elements की संख्या देता है, raw byte distance नहीं — यही exactly है कि standard library same array या container में point करने वाले iterators के बीच distances कैसे compute करती है।
उदाहरण: Subtracting Two Pointers
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {10, 20, 30, 40};
int *ptr1 = &arr[0];
int *ptr2 = &arr[3];
// Print to the console with cout
std::cout << ptr2 - ptr1 << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointers Compare करना
Same array में pointers को उनकी relative positions check करने के लिए <, >, और == से compare किया जा सकता है, जो एक pointer के array के अंत तक पहुँचने का पता लगाने (ptr == arr + size) या indices की बजाय pointers इस्तेमाल करके एक range में चलने वाले loops control करने के लिए एक common technique है।
उदाहरण: Comparing Pointers
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {10, 20, 30};
int *ptr1 = &arr[0];
int *ptr2 = &arr[2];
// Check whether ptr1 < ptr2
if (ptr1 < ptr2) {
// Print to the console with cout
std::cout << "ptr1 comes before ptr2" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
p + 1एक byte move करता है, जब यह pointed-to type केsizeofजितना move करता है, इसलिए typical systems पर एकint*4 bytes आगे बढ़ता है। - एक pointer को एक array के अंत से आगे move करना और इसे dereference करना, जो undefined behavior है।
- उन pointers को subtract करना जो same array में point नहीं करते, जो एक meaningless result देता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: