Theory
જે રહસ્ય arrays એ આખો unit રાખ્યું
નાના નાના રહસ્યો ભેગા થઈ ગયા છે. scanf("%s", name) ને કોઈ & જોઈતું ન હતું. પાછલા lesson ના reveal એ *(p + 2) ને બરાબર marks[2] જેવું વર્તન કરતું બતાવ્યું.
હવે કબૂલાત નો સમય:
Array નું નામ એ values રાખતું variable નથી. એ box zero નો ADDRESS છે.
marks અને &marks[0] એક જ વસ્તુ છે. એક વાર આ દેખાઈ જાય, તો indexing, pointer walking અને C ના design નો અડધો ભાગ એક ચિત્રમાં આવી જાય છે.
Theory
Coach નું નામ જ એની જગ્યા છે
કોઈ ticket checker ને "coach S4" કહો અને એ 60 berths નું ચિત્ર નથી બનાવતો; એ વિચારે છે કે coach ક્યાંથી શરૂ થાય છે. ત્યાંથી, "berth 23" એક નક્કી ચાલ છે. Array નું નામ એ જ રીતે કામ કરે છે: marks શરૂઆતના address ને નામ આપે છે, અને marks[23] નો અર્થ છે "ત્યાંથી શરૂ કરો, 23 boxes ચાલો, box ખોલો". નામ box zero પર રોપેલો એક signpost છે, અને signposts ખસતા નથી.
Theory
એકીકરણ, ઔપચારિક રીતે
int marks[5] = {70, 55, 90, 62, 78};
int *p = marks; /* no & needed: marks IS an address */
C, indexing ને pointer arithmetic તરીકે વ્યાખ્યાયિત કરે છે:
*marks[i] ≡ (marks + i)**
તો આ બધા એક જ box ઉપાડે છે: marks[2], *(marks + 2), *(p + 2), p[2], હા, p[2] પણ: indexing કોઈ પણ pointer પર કામ કરે છે.
એક ફરક જે બચે છે: p એક variable છે (p++ legal, એ ચાલી શકે છે), જ્યારે marks એક constant address છે (marks++ એ compile error છે, signposts ખસતા નથી).
Practical
ચાલતા pointer થી class નો સરવાળો
#include <stdio.h>
int main() {
int marks[5] = {70, 55, 90, 62, 78};
int *p = marks;
int i, total = 0;
for (i = 0; i < 5; i++) {
total += *p; /* read the box */
p++; /* next box */
}
printf("Total: %d\n", total);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
int *p = marks; માં marks ની પહેલાં & વગર આ compile કેમ થાય છે?
- array નું નામ પહેલેથી એના પહેલા element ના address બરાબર હોય છે
- compiler arrays માટે છાનેમાને & મૂકી દે છે
- એ ખરેખર એક error છે જેને compilers સહી લે છે
- arrays ના pointers ને ક્યારેય initialization જોઈતું નથી
Show the answer
array નું નામ પહેલેથી એના પહેલા element ના address બરાબર હોય છે
marks એ જ &marks[0] છે: વ્યાખ્યાથી એક address, એટલે એ સીધું એક pointer માં assign થઈ જાય છે. કંઈ મુકાતું કે માફ કરાતું નથી. આ જ તથ્ય જૂના scanf રહસ્યને ઉકેલે છે: scanf("%s", name) કામ કરે છે કારણ કે name પહેલેથી એક address આપી દે છે, જ્યારે scanf("%d", &marks) ને & જોઈએ કારણ કે એક સાદું int variable આપતું નથી.
Think first
ચાર જોડણી, એક box
int marks[5] = {70, 55, 90, 62, 78}; અને int p = marks; સાથે, મનમાં કાઢો: marks[3], (marks + 3), *(p + 3), p[3]. પછી અઘરો ભાગ: marks++ અને p++ માંથી કયું legal છે?
Show the answer
ચારેય 62 ઉપાડે છે: એ ચાર વેશમાં એક જ expression છે, કારણ કે indexing એ જ *(base + offset) છે.
p++ legal છે (એક pointer variable ચાલે છે); marks++ એ compile error છે (array નું નામ એક constant address છે, રોપેલો signpost). એ legal/illegal જોડી એ array નામ અને pointer વચ્ચેનો સૌથી વધુ quote થતો ફરક છે, અને એક ગેરંટીડ exam line.
Quiz
ચાલતા-pointer વાળા code માં, loop ની ત્રીજી iteration પૂરી થયા પછી (ત્રણ p++ થઈ ચૂક્યા), *p શું આપે છે?
- 62, p હવે marks[3] ને point કરે છે
- 90, p marks[2] પર રહે છે
- 70, p દરેક round શરૂઆતમાં પાછો આવે છે
- Garbage: p array છોડી ચૂક્યો
Show the answer
62, p હવે marks[3] ને point કરે છે
દરેક p++ pointer ને એક box આગળ રોપી દે છે: ત્રણ increments પછી p, marks[3] પર ઊભો છે, 62 રાખતો. એ array ફક્ત પાંચમા increment પછી છોડે છે, જ્યારે loop પહેલેથી થોભી ચૂક્યો હોય છે. એક ચાલતા pointer ની position ને round-દર-round track કરવી એ આ topic નો પ્રમાણભૂત trace સવાલ છે.
Watch out
Marks ક્યાં કપાય છે
જવાબમાં marks++: તરત error, નામ constant છે; એક અલગ pointer થી ચાલો. sizeof ની ગૂંચ: sizeof(marks) એ આખું array છે (અહીં 20 bytes); sizeof(p) એ ફક્ત pointer નું પોતાનું size છે, એ અલગ હોય છે ભલે marks અને p "એક જ જગ્યાએ point કરતા" હોય. અંતના આગળ ચાલવું: loop ની સીમા p ની ચોકી કરે છે; p++ ની ગણતરી ભૂલો અને તમે out of bounds છો, છાનેમાને.
Theory
Unit 3 બંધ થાય છે, અને એક દરવાજો ખૂલે છે
Arrays અને pointers હવે તમારા મનમાં એક જ subject છે, બરાબર જેમ C ઇચ્છે છે. અને Unit 4 નું એક વચન, આગળ: array કોઈ function સુધી એક ADDRESS તરીકે પહોંચે છે, એટલે functions તમારા સાચા data ને બદલી શકે છે, એ જ pass-by-reference ની આખી વાર્તા છે. પણ પહેલાં: function ખરેખર છે શું, અને marks program એમાં વહેંચાવા માટે કેમ તલસે છે.
Summary
Key takeaways
- Array નું નામ જ element zero નો address છે: marks ≡ &marks[0].
- Indexing એ pointer arithmetic છે: marks[i] ≡ *(marks + i); p[i] પણ ચાલે છે.
- int *p = marks; ને & જોઈતું નથી, અને p, p++ થી array પર ચાલી શકે છે.
- marks++ ગેરકાયદે છે (constant address); p++ legal છે (variable).
- sizeof(marks) = આખું array; sizeof(p) = એક pointer.
- scanf("%s", name) સમજાયું: name પહેલેથી એક address છે.
- યાદ રાખવાની યુક્તિ: નામ box zero પર એક signpost છે, અને signposts ખસતા નથી.