Concepts of Pointers

Pointer एक variable है जो value के बजाय एक ADDRESS store करता है: & पूछता है "यह variable कहाँ रहता है?", * पूछता है "इस address पर क्या रहता है?", और यही एक idea आधे C को समझा देता है।

11 min read · 10 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

C का सबसे डरावना शब्द

किसी भी senior से C के बारे में पूछिए और एक शब्द उन्हें आह भरवा देता है: pointers।

यह lesson जिस सच पर टिका है वह यह है: आप पहले से pointers इस्तेमाल कर चुके हैं। हर बार जब आपने scanf("%d", &marks) लिखा, उस & ने scanf को एक address थमाया, और scanf ने उसी के ज़रिए पहुँचाया।

Pointer बस उस idea को एक variable बना देता है: एक box जो यह store करता है कि दूसरा box कहाँ रहता है। एक तस्वीर, दो operators, और डर घुल जाता है।

Theory

घर और पते की पर्ची

आपका घर एक variable है: उसमें चीज़ें होती हैं (उसकी value)। अब अपने घर का पता एक कागज़ की पर्ची पर लिखिए। पर्ची एक pointer है: उसमें आपका फ़र्नीचर नहीं होता, उसमें होता है उसे कहाँ ढूँढें। पर्ची किसी courier को दे दीजिए और वह आपके बिना घर पर हुए भी उसके अंदर delivery कर सकता है। दो सवाल मायने रखते हैं: "आपका पता क्या है?" (&) और "इस पते पर क्या है?" (\*)।

Theory

Pointers, औपचारिक रूप से

हर variable एक numbered memory address पर रहता है (वही addresses जो BCA103 की address bus ढोती है)।

int marks = 78; /* lives at, say, address 5000 */

int *p; /* p is a POINTER to an int */

p = &marks; /* p now holds 5000 */

  • & (address-of): &marks पूछता है "marks कहाँ रहता है?" → 5000।
  • *\ (dereference)*: `p` पूछता है "p के अंदर वाले address पर क्या रहता है?" → 78।

Declaration को ज़ोर से पढ़िए: int *p = "p एक int को point करता है"। p की VALUE एक address है; *p उस address पर की value है।

Practical

दूर से किया गया काम

#include <stdio.h>

int main() {
    int marks = 78;
    int *p = &marks;

    printf("marks = %d\n", marks);
    printf("*p    = %d\n", *p);

    *p = 99;    /* write THROUGH the pointer */
    printf("marks is now %d\n", marks);
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Output का अनुमान लगाइए

Code चलाइए: p, marks का address रखता है, फिर *p = 99 उसके ज़रिए लिखता है। तीन print होने वाली lines क्या हैं?

Show the answer

marks = 78

*p = 78

marks is now 99

तीसरी line ही पूरा lesson है: *\p में लिखने से marks खुद बदल गया*, क्योंकि p और marks एक ही box हैं जिन तक दो रास्तों से पहुँचा जाता है। कुछ copy नहीं हुआ; pointer एक रास्ता है, नकल नहीं। यही दूर-से-किया-गया-काम pointers के होने की वजह है।

Quiz

int marks = 78; int *p = &marks; variable p के अंदर क्या store होता है?

  1. marks का memory address
  2. value 78
  3. marks की एक copy जो अपने-आप update होती है
  4. "marks" नाम, text के रूप में
Show the answer

marks का memory address

Pointer की content एक address ही होती है, और कुछ नहीं: p रखता है कि marks कहाँ रहता है, न कि वह क्या रखता है। Value 78 वह है जो आपको DEREFERENCE (*p) करने पर मिलती है। जो students 78 जवाब देते हैं वे पर्ची को घर से मिला देते हैं, ठीक वही उलझन जिसे रोकने के लिए यह analogy है।

Quiz

इस unit के खुलासे के बाद, scanf को सादे marks के बजाय &marks की ज़रूरत क्यों है?

  1. scanf को input सही box में पहुँचाने के लिए ADDRESS पता होना चाहिए
  2. & से scanf तेज़ चलता है
  3. यह सिर्फ एक naming convention है, कोई मतलब नहीं
  4. & पहले marks को string में बदल देता है
Show the answer

scanf को input सही box में पहुँचाने के लिए ADDRESS पता होना चाहिए

scanf आपके variable में किसी दूसरे function के अंदर से लिखता है, और caller के box तक पहुँचने का इकलौता रास्ता उसका address है, ठीक पर्ची वाला courier। सादा marks, scanf को 78 की copy थमा देता, delivery के लिए बेकार। Unit 1 का रहस्यमय & दरअसल शुरू से एक pointer था।

Watch out

Marks कहाँ कटते हैं

दोनों operators को मिला देना: & एक address देता है, एक को follow करता है; traces में हर step को "address" या "value" label कीजिए। गलत declare करना: `int p एक pointer declare करता है; बाद में, सादा p address है और p` value है, दो भूमिकाएँ निभाता है (declaration बनाम dereference) और exams ठीक यही test करते हैं। लटकते asterisk वाली सोच: p में "marks" नहीं होता, उसमें marks का ADDRESS होता है। यहाँ भाषा की सटीकता marks दिलाती है।

Theory

C यह ताक़त क्यों रखता है

Pointers, functions को आपके variables बदलने देते हैं (pass by reference, Unit 4), programs को arrays पर तेज़ी से चलने देते हैं (array का नाम चुपके से एक address है, दो lessons आगे), और बाद में, Sem 3 में, data structures को runtime पर बढ़ने देते हैं। हर senior जिसने आह भरी, उसने इसी topic पर अपने interviews भी जीते। आगे: व्यावहारिक drill, हर type के pointers declare और initialize करना।

Summary

Key takeaways

  • हर variable एक memory address पर रहता है; pointer एक variable है जो ADDRESS store करता है।
  • & = address-of (यह कहाँ रहता है?); * = dereference (वहाँ क्या रहता है?)।
  • int p = &marks: p, marks का address रखता है; p दूसरे रास्ते से marks की value है।
  • *p = 99 marks को खुद बदल देता है, दूर से किया गया काम।
  • scanf का & आख़िरकार समझ आया: delivery के लिए address चाहिए।
  • याद रखने का hook: घर (variable) और पते की पर्ची (pointer)।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Concepts of Arrays and Pointer

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Concepts of Pointers · Computer Programming and Programming Methodology (CPPM) · Gri-Learn