← Back to C++ Course | Chapter 7: Pointers & References | Lesson 5 of 11

C++ में Pointers और Arrays

एक array का नाम इसके पहले box की ओर एक pointer की तरह act करता है, एक street के starting address जैसा। आप pointer को move करके array में चल सकते हैं।
Syntax
cpp
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

cpp
#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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
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. यह मान लेना कि array name को एक pointer की तरह reassign किया जा सकता है, जैसे arr = p;।
  2. array length ढूंढने के लिए एक pointer पर sizeof इस्तेमाल करना।
  3. dynamic arrays के लिए delete की बजाय delete[] इस्तेमाल करना।

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.