← Back to C Course | Chapter 7: Pointers | Lesson 4 of 9

C के Pointers और Arrays

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

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

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

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

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

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

Pointers के जरिए Array Elements Modify करना

*(ptr + i) = newValue और arr[i] = newValue identical results produce करते हैं -- दोनों आखिरकार एक address compute करते हैं और इस पर लिखते हैं, इसलिए आप कौन सी style इस्तेमाल करते हैं यह फिर से preference और context की बात है।

उदाहरण: Modifying Array Elements via Pointers

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

Pointers Compare करना

दो अलग arrays (या दो unrelated variables) से pointers compare करना C में technically undefined behavior है, भले ही यह अक्सर accidentally 'काम' कर जाए -- सिर्फ उन pointers को compare करें जो दोनों same array के अंदर कहीं या इसके अंत से एक आगे point करते हों।

उदाहरण: Comparing Pointers

c
// 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;
}
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. arr = p; जैसे एक array name को assign करना, जो allowed नहीं है क्योंकि एक array name एक modifiable pointer नहीं है।
  2. array length ढूंढने के लिए एक pointer पर sizeof इस्तेमाल करना, जो इसकी जगह pointer का size देता है।
  3. (*p)++ का मतलब होने पर *p++ लिखना, जो value increment करने की बजाय pointer move करता है।

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.