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 રૂપે p માં રાખેલા address પર રહેલી value આપે છે.

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 ક્યાં રહે છે તે રાખેલું છે, marks માં શું છે તે નહીં. 78 ત્યારે મળે છે જ્યારે તમે એને dereference કરો, *p. Pointer ને અને એ જે value તરફ point કરે છે તેને એક માની લેવું એ સૌથી સામાન્ય ભૂલ છે.

Think first

scanf નું રહસ્ય

scanf("%d", &marks) ને & કેમ જોઈએ? તમારું કારણ type કરો.

Show the answer

scanf એ number તમારા variable માં મૂકવાનો હોય છે, એટલે એને ખબર હોવી જોઈએ કે marks ક્યાં રહે છે. ફક્ત marks આપો તો એને value 78 મળે, જે delivery ના કંઈ કામની નથી. 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 ને ખસેડવાથી ફક્ત p ક્યાં point કરે છે તે બદલાય છે; એનાથી a ક્યારેય બદલાતો નથી.

Summary

Key takeaways

  • Pointer address રાખે છે, value નહીં.
  • & variable નું address આપે છે; * એ address પરની value સુધી પહોંચે છે.
  • *p માં લખવાથી એ variable બદલાય છે જેના તરફ p point કરે છે.
  • યાદ રાખવાની યુક્તિ: & પૂછે ક્યાં, * પૂછે શું.

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