C Pointers परिचय
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Pointers Declare करना
int *ptr, ptr को 'pointer to int' declare करता है, मतलब यह validly सिर्फ एक int variable का address रख सकता है; इसे एक float या char variable का address assign करना एक warning के साथ compile होगा लेकिन बाद में गलत behavior produce करेगा।
उदाहरण: Declaring Pointers
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Address-Of Operator (&)
&count लिखना आपको वह address देता है जहाँ count actually memory में रहता है, जिसे आप फिर आमतौर पर एक pointer variable को assign करते हैं, जैसे int *p = &count -- यह वह operation है जिसका इस्तेमाल आप एक pointer को एक real variable से connect करने के लिए करते हैं।
उदाहरण: The Address-Of Operator (&)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Dereferencing Operator (*)
*ptr = 10 ptr को खुद नहीं बदलता; यह उस address पर store value बदलता है जिसकी ओर ptr currently point कर रहा है -- एक pointer बदलने और यह जिसकी ओर point करता है उसे बदलने के बीच का यह फर्क pointers सीखने के सबसे tricky हिस्सों में से एक है।
उदाहरण: The Dereferencing Operator ()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Null Pointers
एक NULL pointer को dereference करना (जब ptr NULL हो तो *ptr लिखना) एक program crash (segmentation fault) का सबसे common कारणों में से एक है, इसलिए dereference करने से पहले if (ptr != NULL) check करना एक standard defensive habit है।
उदाहरण: Null Pointers
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक pointer को कहीं point करने से पहले इस्तेमाल करना, जैसे
int *p; *p = 5;, जो एक random address पर लिखता है और crash कर सकता है। int* a, b;लिखना और मान लेना कि दोनों pointers हैं, जब सिर्फaएक pointer है औरbएकintहै।&x(x का address) को*p(जिस value की ओर p point करता है) से confuse करना और एक value का इरादा होने पर एक address print कर देना।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: