← Back to C Course | Chapter 7: Pointers | Lesson 3 of 9

C में Pointer Arithmetic

Pointer arithmetic का मतलब है memory के along एक pointer को move करना, एक street पर एक घर से अगले पर step करने जैसा। एक जोड़ना इसके type के अगले item पर move करता है।
Syntax
c
pointer_name++;  // move to the next element
pointer_name--;  // move to the previous element
pointer_name + n;

एक Pointer Increment करना

उदाहरण के लिए, अगर ptr address 1000 पर एक int (ज़्यादातर systems पर 4 bytes) की ओर point करता है, तो ptr++ इसे address 1001 नहीं, 1004 की ओर point कराता है -- compiler automatically increment को sizeof(pointed-to type) से scale करता है।

उदाहरण: Incrementing a Pointer

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {10, 20, 30};
	int *ptr = arr;
	ptr++;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Pointer Decrement करना

increment की तरह ही, ptr-- pointer को exactly एक element जितने bytes पीछे move करता है, यही कारण है कि pointer arithmetic सिर्फ एक single array के अंदर (या इसके अंत से एक आगे) मायने रखता है -- memory में इससे पहले या बाद में क्या है इसकी कोई guarantee नहीं है।

उदाहरण: Decrementing a Pointer

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {10, 20, 30};
	int *ptr = &arr[2];
	ptr--;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pointers में Integers जोड़ना

ptr + n, ptr की current position से n elements आगे चलने के बराबर है, और यह वह mechanism है जो arr[i] और *(arr + i) के बीच equivalence को underlie करता है -- दोनों इसे dereference करने से पहले same address compute करते हैं।

उदाहरण: Adding Integers to Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {10, 20, 30};
	int *ptr = arr;
	// Print formatted text to the screen
	printf("%d", *(ptr + 2));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pointers से Integers Subtract करना

यह addition जैसे ही काम करता है लेकिन reverse में, और आमतौर पर तब इस्तेमाल होता है जब एक array को last element पर शुरू हुए एक pointer से backward iterate किया जाए, पहले की ओर decrement करते हुए।

उदाहरण: Subtracting Integers from Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {10, 20, 30};
	int *ptr = &arr[2];
	// Print formatted text to the screen
	printf("%d", *(ptr - 1));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pointer Subtraction

यह distances compute करने के लिए genuinely useful है -- उदाहरण के लिए, अगर आपके पास एक बड़े array के अंदर एक substring की शुरुआत और अंत का pointer है, उन्हें subtract करना बताता है कि उनके बीच exactly कितने characters हैं।

उदाहरण: Pointer Subtraction

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[5] = {1, 2, 3, 4, 5};
	int *start = &arr[0];
	int *end = &arr[4];
	// Print formatted text to the screen
	printf("%ld", end - start);
	// 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 type के size से move करता है।
  2. एक pointer को एक array के अंत से आगे move करना और इसे dereference करना।
  3. उन pointers को subtract करना जो same array से belong नहीं करते।

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.