Skip to main content

 

Pointer භාවිතයෙ වැදගත් අවස්ථා


  කලින් පාඩම් වලට අදාල ප්‍රශ්න දෙකක්


Q   dynamic memory allocation භාවිතා කිරීමට හේතුව   හා ඊට අදාළ function?

Q2    reference & ඔපරේටර් එක භාවිතා කිරීමට හේතුව


 භාවිතා කරන මූලික අවස්ථා


  • call by reference

  •  dynamic memory allocation

  •  accessing file


call by reference


 මේක තේරුම් ගන්න නම් පහත උදාහරණ දෙක   හොඳින් බලන්න බලන්න.


අගයන් දෙකක් මාරු කර ගැනීම function  එකක් ලිවීම්

Call by value  


  void swap (int a, int b)  //  {

     int c;  // 

     c=a;

     a=b;

     b=c;


}






මෙහිදී අගයන් මාරු වෙන්නේ අදාල ෆන්ෂන් එක තුළ පමණි. Main function එක තුල කිසිවක් වෙනස් වී නොමැත.
ඊට හේතුව නම් main function එක තුළ පවතින variable වෙනත් function එකක සිට මාරු කිරීමට නොහැකි වීමයි


Call by reference

void swap1 (int *a, int *b)

{

    int c;

     c=*a;

    *a=*b;

    *b=c;

  }


Main function එක තුළද අගයන් මාරු වීමකට ලක්වී ඇත


Comments

Popular posts from this blog

C language

               C  Pointers       

C language

How to use a pointer pointer එක භාවිතා කරන ආකාරය                                           int myAge = 23; - අපිට ඕනේ  කරනවා myAge variable එකේ address එක හොයාගන්න. දැනටමත් අපි දන්නවා "&" operate එක භාවිතා කරල ඒක බලාගන්න පුළුවන් කියලා. පහල දාලා තියනවා ඒකට අදාල code එක.                                             printf(" address of myage     %p\n", &myAge); pointer එකක් භාවිතයෙන් variable  එකක address එක ගබඩා කර ගැනීම                                    int myAge = 23;  -  මේ variable එකේ නම myAge ,    data type එක intiger         ...