Concepts of Pointers

Pointer एक variable है जो किसी दूसरे variable का address रखता है: & पूछता है कि variable कहाँ रहता है, और * वहाँ रहने वाली value तक पहुँचता है।

5 min read · 9 cards · 4 checks

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


Theory

marks किसने बदला?

3 lines:

int marks = 78;

int *p = &marks;

*p = 99;

कोई भी line सीधे marks को value नहीं देती। अब marks क्या है?

Think first

पहले आपका अंदाज़ा

आपके हिसाब से marks में जो value है, वह type कीजिए।

Show the answer

99। p में marks का address है, इसलिए *p ख़ुद marks ही है, बस हम उस तक दूसरे रास्ते से पहुँचते हैं। *p में जो लिखा, वह असल में marks में लिखा गया।

Theory

Address और value

हर variable memory के एक numbered address पर रहता है। Pointer एक variable है जो address रखता है: int *p को ऐसे पढ़िए: "p एक int की ओर point करता है"। &, यानी address-of operator, &marks के रूप में marks का address देता है, और *, यानी dereference (value-at) operator, *p के रूप में वह value देता है जो p में रखे address पर है।

Quiz

int marks = 78; int *p = &marks; के बाद p के अंदर क्या रखा है?

  1. marks का memory address
  2. Value 78
  3. marks की एक copy जो ख़ुद update होती है
  4. marks नाम, text के रूप में
Show the answer

marks का memory address

p यह रखता है कि marks कहाँ रहता है, यह नहीं कि उसमें क्या है। 78 तब मिलता है जब आप उसे dereference करते हैं, *p। Pointer को और जिस value की ओर वह point करता है, उन दोनों को एक समझ लेना सबसे आम ग़लती है।

Think first

scanf का रहस्य

scanf("%d", &marks) को & क्यों चाहिए? अपनी वजह type कीजिए।

Show the answer

scanf को number आपके variable में रखना होता है, इसलिए उसे पता होना चाहिए कि marks कहाँ रहता है। सिर्फ़ marks देने से उसे बस value 78 मिलती, जिससे यह पता ही नहीं चलता कि number कहाँ रखना है। Unit 1 से आप जो & लिख रहे हैं, वह हमेशा से एक address ही था, और इसी तरीके से functions आपके variables बदल पाते हैं।

Watch out

एक star, दो काम

Declaration में int *p, p को pointer बनाता है। Expression में *p वह value लाता है जिसकी ओर p point करता है, इसलिए p address है और *p value।

Practical

Exam-style: output बताइए

#include <stdio.h>

int main() {
    int a = 5, b = 10;
    int *p = &a;

    *p = *p + 1;
    p = &b;
    *p = *p * 2;

    printf("%d %d", a, b);
    return 0;
}

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

Quiz

Exam-style: ऊपर वाले program का output क्या है?

  1. 6 20
  2. 5 20
  3. 6 10
  4. 12 10
Show the answer

6 20

पहले p, a की ओर point करता है, इसलिए *p = *p + 1 से a 6 हो जाता है। फिर p, b पर जाता है, और *p = *p * 2 से b 20 हो जाता है। p को b पर ले जाने से सिर्फ़ यह बदलता है कि वह कहाँ point करता है; इससे a कभी नहीं बदलता।

Summary

Key takeaways

  • Pointer address रखता है, value नहीं।
  • & variable का address देता है; * उस address पर रखी value तक पहुँचता है।
  • *p में लिखने से वह variable बदलता है जिसकी ओर p point करता है।
  • याद रखने का hook: & पूछता है कहाँ, * पूछता है क्या।

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