← Back to C++ Course | Chapter 7: Pointers & References | Lesson 4 of 11

C++ में Pointer Arithmetic

Pointer arithmetic का मतलब है memory के along एक pointer को move करना, एक street पर एक घर से अगले पर step करने जैसा। एक जोड़ना इसके type के अगले item पर move करता है।
Syntax
cpp
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

cpp
// 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;
}

एक Pointer Decrement करना

ptr-- से decrement करना reverse में same तरह काम करता है, pointer को एक single raw byte की बजाय एक पूरे element जितने bytes पीछे step कराता है, जो एक array में सही तरीके से backward चलने के लिए essential है।

उदाहरण: Decrementing a Pointer

cpp
// 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;
}

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

cpp
// 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;
}

दो 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

cpp
// 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;
}

Pointers Compare करना

Same array में pointers को उनकी relative positions check करने के लिए <, >, और == से compare किया जा सकता है, जो एक pointer के array के अंत तक पहुँचने का पता लगाने (ptr == arr + size) या indices की बजाय pointers इस्तेमाल करके एक range में चलने वाले loops control करने के लिए एक common technique है।

उदाहरण: Comparing Pointers

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि p + 1 एक byte move करता है, जब यह pointed-to type के sizeof जितना move करता है, इसलिए typical systems पर एक int* 4 bytes आगे बढ़ता है।
  2. एक pointer को एक array के अंत से आगे move करना और इसे dereference करना, जो undefined behavior है।
  3. उन pointers को subtract करना जो same array में point नहीं करते, जो एक meaningless result देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.