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 address 1000 पर एक int (ज़्यादातर systems पर 4 bytes) की ओर point करता है, तो ptr++ इसे address 1001 नहीं, 1004 की ओर point कराता है -- compiler automatically increment को sizeof(pointed-to type) से scale करता है।
उदाहरण: Incrementing a Pointer
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक Pointer Decrement करना
increment की तरह ही, ptr-- pointer को exactly एक element जितने bytes पीछे move करता है, यही कारण है कि pointer arithmetic सिर्फ एक single array के अंदर (या इसके अंत से एक आगे) मायने रखता है -- memory में इससे पहले या बाद में क्या है इसकी कोई guarantee नहीं है।
उदाहरण: Decrementing a Pointer
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Pointers में Integers जोड़ना
ptr + n, ptr की current position से n elements आगे चलने के बराबर है, और यह वह mechanism है जो arr[i] और *(arr + i) के बीच equivalence को underlie करता है -- दोनों इसे dereference करने से पहले same address compute करते हैं।
उदाहरण: Adding Integers to Pointers
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Pointers से Integers Subtract करना
यह addition जैसे ही काम करता है लेकिन reverse में, और आमतौर पर तब इस्तेमाल होता है जब एक array को last element पर शुरू हुए एक pointer से backward iterate किया जाए, पहले की ओर decrement करते हुए।
उदाहरण: Subtracting Integers from Pointers
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Pointer Subtraction
यह distances compute करने के लिए genuinely useful है -- उदाहरण के लिए, अगर आपके पास एक बड़े array के अंदर एक substring की शुरुआत और अंत का pointer है, उन्हें subtract करना बताता है कि उनके बीच exactly कितने characters हैं।
उदाहरण: Pointer Subtraction
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
p + 1एक byte move करता है, जब यह pointed type के size से move करता है। - एक pointer को एक array के अंत से आगे move करना और इसे dereference करना।
- उन pointers को subtract करना जो same array से belong नहीं करते।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: