C++ में Pointers और Arrays
In this page:
data_type array_name[] = {value1, value2, value3};
data_type *pointer_name = array_name;
*(pointer_name + index);
Pointer की तरह Array Name
C++ में, एक array का नाम इसके पहले element का एक constant pointer की तरह act करता है, इसलिए आप address-of operator इस्तेमाल किए बिना array नाम को सीधे एक pointer variable को assign कर सकते हैं।
यही वजह है कि arrays accept करने वाले functions actually एक pointer receive करते हैं, पूरे block की एक copy नहीं।
उदाहरण: Array Name as Pointer
#include <iostream>
int main() {
int arr[] = {1, 2, 3};
int *ptr = arr; // array name decays to a pointer to its first element
std::cout << *ptr << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointer Arithmetic
आप एक array में navigate करने के लिए pointer arithmetic इस्तेमाल कर सकते हैं: ptr++ से एक pointer increment करना इसे exactly sizeof(type) bytes आगे move करता है, एक byte नहीं, इसलिए compiler automatically jump को element type के हिसाब से scale करता है।
यह आपको कभी एक index variable लिखे बिना एक buffer में चलने देता है।
उदाहरण: Pointer Arithmetic
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {1, 2, 3};
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;
}
Login to try C/C++/Java/PHP code in the editor
Pointers से Array Elements Access करना
arr[i] जैसे brackets इस्तेमाल करने की बजाय, आप किसी array में कोई भी element पढ़ने या modify करने के लिए *(ptr + i) जैसे dereferenced pointer offsets इस्तेमाल कर सकते हैं, और असल में arr[i] को language exactly उसी expression के shorthand की तरह define करती है।
यह equivalence जानना pointer-heavy legacy C++ code का बहुत कुछ explain करता है।
उदाहरण: Accessing Array Elements with Pointers
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {10, 20, 30};
int *ptr = arr;
// Print to the console with cout
std::cout << *(ptr + 1) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dynamically Allocated Arrays
आप 'new[]' operator इस्तेमाल करके heap पर dynamic arrays allocate कर सकते हैं, जो आपको compile time की बजाय runtime पर array size set करने देता है।
खत्म होने पर 'delete[]' इस्तेमाल करके यह memory release करना याद रखें, क्योंकि brackets या पूरी call भूल जाना चुपचाप memory leak करता है।
उदाहरण: Dynamically Allocated Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare size and set it to 3
int size = 3;
// Allocate an object on the heap with new
int *arr = new int[size];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
// Print to the console with cout
std::cout << arr[1] << std::endl;
// Free the heap memory with delete
delete[] arr;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointers इस्तेमाल करके Functions को Arrays Pass करना
जब आप एक array को एक function को pass करते हैं, यह copy होने की बजाय इसके पहले element के एक pointer में decay हो जाता है, जो efficient है लेकिन इसका मतलब यह भी है कि function array की size information खो देता है।
यही वजह है कि array-processing functions आमतौर पर pointer के साथ एक explicit length parameter लेते हैं।
उदाहरण: Passing Arrays to Functions using Pointers
// Include std::cout and std::cin
#include <iostream>
void printFirst(int *arr) {
// Print to the console with cout
std::cout << arr[0] << std::endl;
}
// Program execution starts in main()
int main() {
int nums[] = {5, 10, 15};
printFirst(nums);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि array name को एक pointer की तरह reassign किया जा सकता है, जैसे
arr = p;। - array length ढूंढने के लिए एक pointer पर
sizeofइस्तेमाल करना। - dynamic arrays के लिए
deleteकी बजायdelete[]इस्तेमाल करना।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: