Pointer arithmetic

Pointer arithmetic BOXES માં ગણે છે, bytes માં નહીં: p+1 એક આખા element ને કૂદે છે (int* માટે 4 bytes, char* માટે 1), p++ આગળના element સુધી ચાલે છે, અને બે pointers ને બાદ કરવાથી એમની વચ્ચેના elements ગણાય છે.

10 min read · 10 cards · 3 checks

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


Theory

જે સવાલ અડધા class ને પાડી દે છે

એક int pointer p, address 5000 રાખે છે. p + 1 શું છે?

દરેક class ના અડધા લોકો 5001 જવાબ આપે છે, અને mark ગુમાવે છે.

Machine નો જવાબ: 5004. કારણ કે p એ int* છે, અને એક int 4 bytes રોકે છે, C એ નક્કી કર્યું કે pointer arithmetic ને આખા boxes માં ગણવું જોઈએ, bytes માં નહીં. એ જ એક design choice આજનો lesson છે, અને એ જ કારણ છે કે pointers અને arrays એક જ topic બનવાના છે.

Theory

ઈંટોના નહીં, ઘરોના આગળ ચાલવું

કોઈ શેરીમાં ચાલતાં, "આગળનું ઘર" નો અર્થ "જમણી બાજુ એક ઈંટ" નથી થતો: એનો અર્થ છે એક આખો plot આગળ, plot નું size જે હોય તે. Pointer arithmetic એ જ રીતે ચાલે છે: p + 1 નો અર્થ p ના type નો આગળનો box છે. Int ના plot 4 bytes પહોળા છે, char ના 1, એટલે એ જ +1 stride અલગ અલગ અંતર કાપે છે. Pointer નો type એ જ ચબરખી પર plot નું size છે.

Theory

Arithmetic, ઔપચારિક રીતે

એક pointer p, જેનો type T* છે, અને sizeof(T) = s:

  • p + n → n boxes આગળનો address: સંખ્યામાં p + n×s bytes.
  • p - n → n boxes પાછળ.
  • p++ / p-- → એક box આગળ/પાછળ પગલું (ચાલવાનું engine).
  • q - p (બંને એક જ array માં) → એમની વચ્ચે કેટલા ELEMENTS છે, bytes નહીં.

અર્થહીન અને ગેરકાયદે: p + q (બે addresses ઉમેરવાથી કંઈ જવાબ મળતો નથી), અને pointers ને ગુણવા કે ભાગવા. C બરાબર એ જ operations આપે છે જે boxes ની શેરી પર કોઈ અર્થ રાખે છે.

Practical

જુઓ strides કેવી રીતે અલગ છે

#include <stdio.h>

int main() {
    int  marks[3] = {70, 80, 90};
    int  *p = &marks[0];
    char word[] = "abc";
    char *c = &word[0];

    printf("p   = %p\n", (void *)p);
    printf("p+1 = %p\n", (void *)(p + 1));   /* +4 bytes */
    printf("c   = %p\n", (void *)c);
    printf("c+1 = %p\n", (void *)(c + 1));   /* +1 byte  */
    printf("*(p+2) = %d\n", *(p + 2));
    return 0;
}

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

Quiz

એક float pointer pf, address 6000 રાખે છે (floats 4 bytes ના હોય છે). pf + 3 કયો address છે?

  1. 6012
  2. 6003
  3. 6004
  4. 6300
Show the answer

6012

ત્રણ float-boxes આગળ: 6000 + 3×4 = 6012. 6003 જવાબ આપવો એ boxes ને બદલે bytes ગણવા છે, એ જ ક્લાસિક ભૂલ જેને મારવા માટે આ આખો lesson છે. દરેક આવા સવાલનું formula: address + n × sizeof(type). char* સાથે બંને જવાબ એક થઈ જાય છે (stride 1), એટલે જ exams int અને float પસંદ કરે છે.

Think first

*(p + 2) ને સમજો

ઉપરના code માં, p, {70, 80, 90} ના marks[0] ને point કરે છે. બંને operators ના અર્થ વાપરીને *(p + 2) ને મોટેથી વાંચો, પછી અનુમાન કરો કે એ શું print કરશે.

Show the answer

"p થી બે int-boxes આગળ જાઓ, પછી ત્યાં રહેતી value ઉપાડો." marks[0] થી બે boxes આગળ marks[2] છે, તો એ 90 print કરે છે.

આની સાથે થોભો: *(p + 2) એ બરાબર marks[2] જેવું વર્તન કર્યું. એ યોગાનુયોગ નથી, એ આગળના lesson ની હેડલાઇન છે. તમે હમણાં pointer ના કપડાંમાં લખેલી array indexing વાપરી છે.

Quiz

p, એક જ int array ના marks[1] ને point કરે છે અને q, marks[4] ને. q - p શું છે?

  1. 3, એમની વચ્ચે elements ની સંખ્યા
  2. 12, એમની વચ્ચે bytes ની સંખ્યા
  3. 5, મોટો index
  4. ગેરકાયદે: pointers ને બાદ કરી શકાતા નથી
Show the answer

3, એમની વચ્ચે elements ની સંખ્યા

Pointer નો તફાવત element ના size થી નીચે scale થાય છે, જે કામનો સવાલ "કેટલા boxes દૂર?" નો જવાબ આપે છે: 4 - 1 = 3. Byte નો ફરક (12) એ કાચી arithmetic છે જેને C તમારાથી છુપાવે છે. અને આ અસમાનતા ધ્યાનમાં લો જેને exams તપાસે છે: p - q એક array ની અંદર legal છે; p + q અર્થહીન અને ગેરકાયદે છે.

Watch out

Marks ક્યાં કપાય છે

Byte ગણવા: int પર p+1, 4 ચાલે છે, 1 નહીં; દરેક જવાબમાં ×sizeof વાળું step લખો. Pointers ઉમેરવા: p + q ગેરકાયદે છે, ફક્ત તફાવત અર્થ રાખે છે. શેરીની બહાર ચાલવું: array ના અંત ના આગળ p++ એ out-of-bounds છે, pointer વાળા લહેકામાં, એ જ છાનેમાને થતી corruption જે marks[n] માં થાય છે. અને (void ) cast સાથે %p, એ address PRINT કરવાની પ્રામાણિક રીત છે, lab work માં વાપરવા લાયક.

Theory

આગળનો ખુલાસો

તમે reveal માં પકડી લીધું: *(p + 2) એ જ marks[2] છે. આગળનો lesson એને official બનાવે છે: array નું NAME એ box zero નો address છે, indexing એ વેશપલટો કરેલી pointer arithmetic છે, અને scanf("%s", name) નો & રહસ્ય કાયમ માટે બંધ થઈ જાય છે. આ unit ના બે સૌથી મોટા idea હાથ મિલાવવાના છે.

Summary

Key takeaways

  • Pointer arithmetic boxes માં ગણે છે: p + n, n × sizeof(type) bytes આગળ વધે છે.
  • int 5000 પર: p+1 એ 5004; char 1 ચાલે છે; float* 4.
  • p++ box-દર-box ચાલે છે, traversal engine.
  • q - p (એક જ array) એમની વચ્ચેના ELEMENTS ગણે છે; p + q ગેરકાયદે છે.
  • *(p + 2) બે boxes આગળની value ઉપાડે છે, અને marks[2] બરાબર છે.
  • યાદ રાખવાની યુક્તિ: આગળનું ઘર, આગળની ઈંટ નહીં.

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

Pointer arithmetic · Computer Programming and Programming Methodology (CPPM) · Gri-Learn