C के Pointers और Arrays
In this page:
data_type array_name[] = {value1, value2, value3};
data_type *pointer_name = array_name;
*(pointer_name + index); // same as array_name[index]
एक Pointer की तरह Array Name
यही वजह है कि sizeof(arr) और sizeof(arr[0]) अलग-अलग behave करते हैं भले ही arr खुद expressions में एक pointer 'जैसा दिखे' -- array name सिर्फ ज़्यादातर expressions में इस्तेमाल होने पर एक actual pointer value में decay होता है, लेकिन sizeof अभी भी पूरा array type देखता है।
उदाहरण: Array Name as a Pointer
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int arr[3] = {1, 2, 3};
// Print formatted text to the screen
printf("%d", *arr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointers इस्तेमाल करके Elements Access करना
दोनों notations exact same machine instructions में compile होते हैं, इसलिए arr[i] और *(arr + i) के बीच चुनना purely readability की बात है -- ज़्यादातर C code performance-sensitive pointer-walking code को छोड़कर clarity के लिए bracket notation पसंद करता है।
उदाहरण: Accessing Elements using Pointers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int arr[3] = {10, 20, 30};
// Print formatted text to the screen
printf("%d", *(arr + 1));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointer Increments से Arrays Scan करना
यह pattern (ptr = arr; while (...) { ...; ptr++; }) हर access पर scratch से एक address दोबारा compute करने से बचता है जैसे arr[i] technically करता है, जो पुराने compilers पर ज़्यादा मायने रखता था जो modern ones जितनी aggressively indexing optimize नहीं करते थे।
उदाहरण: Scanning Arrays with Pointer Increments
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int arr[3] = {1, 2, 3};
int *ptr = arr;
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
// Print formatted text to the screen
printf("%d ", *ptr);
ptr++;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointers के जरिए Array Elements Modify करना
*(ptr + i) = newValue और arr[i] = newValue identical results produce करते हैं -- दोनों आखिरकार एक address compute करते हैं और इस पर लिखते हैं, इसलिए आप कौन सी style इस्तेमाल करते हैं यह फिर से preference और context की बात है।
उदाहरण: Modifying Array Elements via Pointers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int arr[3] = {1, 2, 3};
int *ptr = arr;
*(ptr + 1) = 99;
// Print formatted text to the screen
printf("%d", arr[1]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointers Compare करना
दो अलग arrays (या दो unrelated variables) से pointers compare करना C में technically undefined behavior है, भले ही यह अक्सर accidentally 'काम' कर जाए -- सिर्फ उन pointers को compare करें जो दोनों same array के अंदर कहीं या इसके अंत से एक आगे point करते हों।
उदाहरण: Comparing Pointers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int arr[3] = {1, 2, 3};
int *p1 = &arr[0];
int *p2 = &arr[2];
// Print formatted text to the screen
printf("%d", p1 < p2);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
arr = p;जैसे एक array name को assign करना, जो allowed नहीं है क्योंकि एक array name एक modifiable pointer नहीं है।- array length ढूंढने के लिए एक pointer पर
sizeofइस्तेमाल करना, जो इसकी जगह pointer का size देता है। (*p)++का मतलब होने पर*p++लिखना, जो value increment करने की बजाय pointer move करता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: