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

C Pointers परिचय

एक pointer एक variable है जो दूसरे variable का address रखता है, यह बताने वाले एक note की तरह कि कोई चीज़ किस locker में है। आप उस चीज़ तक पहुंचने के लिए note follow करते हैं।
Syntax
c
data_type *pointer_name = &variable_name;

*pointer_name;  // the value the pointer points to

Pointer क्या है?

यह indirection dynamic memory, arrays, और functions के बीच बड़ा data efficiently pass करने की foundation है -- एक पूरा struct या array copy करने की बजाय, आप सिर्फ इसका address pass कर सकते हैं, जो हमेशा एक fixed, छोटा size होता है।

उदाहरण: What is a Pointer?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 10
	int x = 10;
	int *ptr = &x;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pointers Declare करना

int *ptr, ptr को 'pointer to int' declare करता है, मतलब यह validly सिर्फ एक int variable का address रख सकता है; इसे एक float या char variable का address assign करना एक warning के साथ compile होगा लेकिन बाद में गलत behavior produce करेगा।

उदाहरण: Declaring Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare count and set it to 5
	int count = 5;
	int *ptr = &count;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Address-Of Operator (&)

&count लिखना आपको वह address देता है जहाँ count actually memory में रहता है, जिसे आप फिर आमतौर पर एक pointer variable को assign करते हैं, जैसे int *p = &count -- यह वह operation है जिसका इस्तेमाल आप एक pointer को एक real variable से connect करने के लिए करते हैं।

उदाहरण: The Address-Of Operator (&)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare count and set it to 5
	int count = 5;
	int *p = &count;
	// Print formatted text to the screen
	printf("%p", (void*)p);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Dereferencing Operator (*)

*ptr = 10 ptr को खुद नहीं बदलता; यह उस address पर store value बदलता है जिसकी ओर ptr currently point कर रहा है -- एक pointer बदलने और यह जिसकी ओर point करता है उसे बदलने के बीच का यह फर्क pointers सीखने के सबसे tricky हिस्सों में से एक है।

उदाहरण: The Dereferencing Operator ()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	int *ptr = &x;
	*ptr = 10;
	// Print formatted text to the screen
	printf("%d", x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Null Pointers

एक NULL pointer को dereference करना (जब ptr NULL हो तो *ptr लिखना) एक program crash (segmentation fault) का सबसे common कारणों में से एक है, इसलिए dereference करने से पहले if (ptr != NULL) check करना एक standard defensive habit है।

उदाहरण: Null Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int *ptr = NULL;
	// Check whether ptr != NULL
	if (ptr != NULL) {
		// Print formatted text to the screen
		printf("%d", *ptr);
	} else {
		// Print formatted text to the screen
		printf("Pointer is null");
	}
	// 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. एक pointer को कहीं point करने से पहले इस्तेमाल करना, जैसे int *p; *p = 5;, जो एक random address पर लिखता है और crash कर सकता है।
  2. int* a, b; लिखना और मान लेना कि दोनों pointers हैं, जब सिर्फ a एक pointer है और b एक int है।
  3. &x (x का address) को *p (जिस value की ओर p point करता है) से confuse करना और एक value का इरादा होने पर एक address print कर देना।

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.