Notes in Hindi

Concept of Pointer Arithmetic in Hindi

/ BCA / Programming with C and CPP

Pointer Arithmetic Concepts Explained

Concept of Pointer Arithmetic in Hindi

Pointer Arithmetic क्या होता है?

Pointer Arithmetic का मतलब होता है pointer के साथ कुछ mathematical operations करना। चूँकि pointer किसी variable के memory address को store करता है, इसलिए जब हम pointer पर operations करते हैं, तो हम सीधे memory में movement कर रहे होते हैं। यह feature खासतौर पर array के साथ काम करते समय बहुत काम आता है।

Pointer Arithmetic के मुख्य प्रकार

  • Increment (++)
  • Decrement (--)
  • Addition with Integer (ptr + n)
  • Subtraction with Integer (ptr - n)
  • Pointer Subtraction (ptr1 - ptr2)

Pointer Arithmetic को समझने के लिए एक Example:

मान लीजिए हमारे पास एक integer array है:

int arr[3] = {10, 20, 30}; int *ptr = arr;

यहाँ ptr, arr[0] का address रखता है। अगर हम ptr++ करेंगे, तो यह arr[1] की तरफ move कर जाएगा। इसका कारण है कि ptr integer type का pointer है, और integer की size 4 bytes होती है (32-bit system पर)।


Increment and Decrement in Pointer Arithmetic in Hindi

Pointer Increment क्या है?

जब आप एक pointer को increment (++) करते हैं, तो वो अपने type के अनुसार अगले memory block पर चला जाता है।

Example:

int arr[3] = {10, 20, 30}; int *ptr = arr; ptr++; // अब ptr arr[1] को point करेगा

यहाँ ptr एक int type pointer है, इसलिए यह 4 bytes आगे बढ़ेगा।

Pointer Decrement क्या है?

अगर आप pointer को decrement (-- ) करते हैं, तो वो पिछले memory block की ओर चला जाता है।

Example:

int arr[3] = {10, 20, 30}; int *ptr = &arr[2]; ptr--; // अब ptr arr[1] को point करेगा

Addition-Subtraction of Integer with Pointer in Hindi

Pointer में Integer जोड़ना

Pointer Arithmetic का एक बड़ा उपयोग integer के साथ addition/subtraction करना होता है। जब आप किसी pointer में integer जोड़ते हैं, तो वो pointer memory में आगे की तरफ move करता है।

Example:

int arr[5] = {10, 20, 30, 40, 50}; int *ptr = arr; ptr = ptr + 3; // अब ptr arr[3] को point करेगा

यहाँ ptr तीन positions आगे बढ़ गया, यानी ptr अब arr[3] यानी 40 को point कर रहा है।

Pointer में Integer घटाना

Pointer में subtraction करने से pointer पिछले memory blocks की ओर move करता है।

Example:

int arr[5] = {10, 20, 30, 40, 50}; int *ptr = &arr[4]; ptr = ptr - 2; // अब ptr arr[2] यानी 30 को point करेगा

Pointer Subtraction (दो pointers के बीच subtraction)

अगर दो pointers एक ही array में हैं, तो आप उनका अंतर निकाल सकते हैं, जिससे यह पता चलता है कि वे कितने elements दूर हैं।

Example:

int arr[5] = {10, 20, 30, 40, 50}; int *ptr1 = &arr[4]; int *ptr2 = &arr[1]; int diff = ptr1 - ptr2; // output: 3

मतलब ptr1 और ptr2 के बीच 3 elements का gap है।


Pointer Arithmetic with Arrays in Hindi

Array और Pointer का संबंध

C में array का नाम खुद एक pointer की तरह होता है जो array के पहले element का address रखता है। इसलिए हम array पर pointer arithmetic आसानी से कर सकते हैं।

Example:

int arr[4] = {5, 10, 15, 20}; int *ptr = arr; printf("%d", *(ptr + 2)); // Output: 15

यहाँ *(ptr + 2) का मतलब है arr[2], क्योंकि ptr array के पहले element को point कर रहा है।

Pointer का इस्तेमाल करके Array Traverse करना

int arr[5] = {1, 2, 3, 4, 5}; int *ptr = arr; for(int i = 0; i < 5; i++) { printf("%d ", *(ptr + i)); }

इस तरह हम array के सभी elements को pointer arithmetic की मदद से access कर सकते हैं।

Table: Pointer Arithmetic vs Array Indexing

Array Indexing Pointer Arithmetic
arr[2] *(arr + 2)
arr[i] *(arr + i)
for loop with index for loop with pointer increment

Array Traversal by Incrementing Pointer

int arr[5] = {10, 20, 30, 40, 50}; int *ptr = arr; for(int i = 0; i < 5; i++) { printf("%d ", *ptr); ptr++; }

इस तरीके में हम सीधे pointer को increment करते हैं, जिससे वो हर बार अगले element को point करता है।

FAQs

Pointer Arithmetic का मतलब होता है किसी pointer में mathematical operations जैसे increment, decrement, addition या subtraction करना। इससे हम memory में आगे या पीछे move कर सकते हैं। यह खासतौर पर arrays के साथ उपयोगी होता है।
जब आप किसी pointer को increment (++) करते हैं, तो वो अपने data type के अनुसार memory में अगले address पर move करता है। जैसे int pointer 4 bytes आगे जाएगा, float pointer 4 bytes और char pointer 1 byte आगे बढ़ेगा।
हाँ, pointer में integer जोड़ने से pointer memory में आगे बढ़ता है। उदाहरण के लिए, अगर ptr एक int pointer है और आप ptr + 2 करते हैं, तो ptr दो elements आगे बढ़ जाएगा (2 × 4 bytes)।
Pointer subtraction से हम यह पता कर सकते हैं कि दो pointers के बीच कितने elements का अंतर है, बशर्ते दोनों pointers एक ही array में हों। यह array में position difference जानने में मदद करता है।
Arrays में pointer arithmetic से हम किसी भी element को access कर सकते हैं जैसे *(arr + i)। यह array indexing का ही alternate तरीका है। इससे हम arrays को pointers के ज़रिए efficiently traverse कर सकते हैं।

Please Give Us Feedback