Declaring and initializing int, float, char and void pointers

દરેક pointer નો type એના target સાથે મેળ ખાય છે, NULL એટલે હજી ક્યાંય point નથી કરતો, અને void pointer કોઈ પણ address રાખી શકે પણ વાપરતા પહેલાં cast કરવો પડે.

5 min read · 10 cards · 4 checks

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


Theory

કયો જોખમી છે?

3 pointers:

int *a = &marks;

int *b = NULL;

int *c;

c ને ક્યારેય કોઈ value મળતી નથી. કોને dereference કરવું સૌથી જોખમી છે?

Think first

પહેલાં તમારો અંદાજ

Letter અને તમારું કારણ type કરો.

Show the answer

c. એમાં જૂનો garbage પડેલો છે, એટલે *c કોઈ અજાણી જગ્યાને અડે છે: એક wild pointer. *b પણ એક error છે, પણ if (b != NULL) થી પહેલાં ચકાસી શકાય. દરેક pointer ને initialize કરો, કોઈ target થી કે NULL થી.

Theory

દરેક type નો પોતાનો pointer

int *pi = &marks;, float *pf = &perc; અને char *pc = &grade; ત્રણેય પોતાના target સાથે મેળ ખાય છે. Type જરૂરી છે, કારણ કે dereference એના પરથી જ જાણે છે કે કેટલા bytes વાંચવા અને કેવી રીતે વાંચવા: int તરીકે સામાન્ય રીતે 4 bytes, char તરીકે 1 byte.

Practical

Pointer drill

#include <stdio.h>

int main() {
    int marks = 78;
    float perc = 78.5;
    char grade = 'A';

    int *pi = &marks;
    float *pf = &perc;
    char *pc = &grade;

    printf("%d %.1f %c", *pi, *pf, *pc);
    return 0;
}

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

Quiz

Float ના pointer ને int * ને બદલે float * કેમ declare કરવો પડે છે?

  1. Type પરથી * જાણે છે કે કેટલા bytes અને કેવી રીતે વાંચવા
  2. એ ફક્ત નામ રાખવાની રીત છે
  3. Float pointers memory ના એક અલગ, ખાસ ભાગમાં રાખવામાં આવે છે
  4. int pointer મોટું address રાખી ન શકે
Show the answer

Type પરથી * જાણે છે કે કેટલા bytes અને કેવી રીતે વાંચવા

દરેક pointer એક address રાખે છે; type, * ને કહે છે કે એને કેવી રીતે વાંચવું. ખોટો type સાચી જગ્યાને ખોટી રીતે વાંચે છે: garbage.

Theory

NULL અને void *

NULL એટલે "હજી ક્યાંય point નથી કરતો", અને code એને ચકાસી શકે છે. void *vp, એટલે કે generic pointer, કોઈ પણ type નું address રાખી શકે, પણ cast કર્યા વગર એને dereference ન કરી શકાય: *(int *)vp.

Think first

Pointers ગણો

int* a, b; કેટલા pointers declare કરે છે?

Show the answer

એક: a pointer છે, પણ b ફક્ત એક int છે. * નામ સાથે જોડાય છે, type સાથે નહીં, એટલે 2 pointers માટે int *a, *b; લખો.

Practical

Exam-style: output કહો

#include <stdio.h>

int main() {
    int x = 7;
    void *vp = &x;
    int *ip = (int *)vp;

    *ip = *ip + 3;
    printf("%d %d", x, *(int *)vp);
    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. 10 10
  2. 7 10
  3. 10 7
  4. Compile error
Show the answer

10 10

vp અને ip બંનેમાં x નું address છે. ip દ્વારા 3 ઉમેરવાથી x 10 થાય છે, અને *(int *)vp એ જ box વાંચે છે.

Summary

Key takeaways

  • Pointer નો type target સાથે મેળવો: int *, float *, char *.
  • દરેક pointer ને initialize કરો, target થી કે NULL થી; એને ક્યારેય wild ન રહેવા દો.
  • void * કોઈ પણ address રાખે છે, પણ * વાપરતાં પહેલાં એને cast કરવો પડે.
  • યાદ રાખવાની યુક્તિ: ખાલી ચિઠ્ઠી, ગમે તે address લખેલી ચિઠ્ઠી કરતાં સારી.

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

Declaring and initializing int, float, char and void pointers · Computer Programming and Programming Methodology (CPPM) · Gri-Learn