POINTER AND FUNCTIONS(Указатели и функции)


Pointer and functions(Указатели и  функции) 

Function မ်ားသည္ pointer မ်ားအား ၎တို႕၏ argument အၿဖစ္အသံုးၿပဳနိဳင္ၿပီး function အား calling လုပ္ၿခင္းၿဖင့္ return ၿပန္နိဳင္သည္။C++ language တြင္ pointer အတြက္ modify ၿပဳလုပ္ထားေသာ ပံုစံရွိသည္။၎အား reference ဟုေခၚသည္။reference မ်ားအား function ၏ parameter အၿဖစ္ မ်ားစြာအသံုးၿပဳၾကသည္။

Methods of passing parameters
Argument မ်ားအား function တြင္ passing ၿပဳလုပ္ရာတြင္ ေအာက္ေဖာ္ၿပပါ နည္းမ်ားအား ေတြ႕နိဳင္သည္။
by value
by pointer
by reference
အကယ္၍ function သည္ ၎၏အလုပ္အား process လုပ္ေနစဥ္အခ်ိန္တြင္ argument ၏တန္ဖိုးအား ေၿပာင္းလဲရန္လိုအပ္ပါက by reference (သို႕) by pointer နည္းအားအသံုးၿပဳသည္။

ထိုအတူ by value နည္းၿဖင့္ argument အား pass ၿပဳလုပ္ရာ တြင္လည္း stack memory ေပၚတြင္ ၎အား copy ၿပဳလုပ္ၿခင္း ၿဖစ္ေပၚနိဳင္သည္ၿဖစ္သည္။function အလုပ္လုပ္ၿပီးသြားခ်ိန္တြင္ function ၏ argument မ်ားရွိေသာ stack ၏ space သည္ clean ၿဖစ္သြားမည္ၿဖစ္ၿပီး ၎ေပၚရွိ data မ်ားသည္ ပ်က္စီးသြားမည္ၿဖစ္သည္။function ၏ မူလတန္ဖိုးသည္လည္း ရရွိနိဳင္ေတာ့္မည္မဟုတ္ေပ။

Example  Passing parameters to functions by value, by reference, and used pointer.
# include <iostream>
using namespace std;
# include <conio.h>

int Square_Val (int i) / / parameter - by value
{  i++;
return i * i;
}
void Square_Ref (int & i) / / parameter - by reference
{ i= i * i;
}
void Square_Ptr (int * i) / / parameter is passed as a pointer
{ *i = *i  * *i;
}

int main ()
{ int v = 5, s, p = 5;
s = Square_Val (v);
cout << "s =" << s << "v =" << v << endl;               / / S = 36 v = 5
Square_Ref (p);
cout << "p =" << p << endl;                                           / / P = 25
Square_Ptr (& p);
cout << "p =" << p << endl;                                           / / P = 625
getch ();
return 0;
}

Passing arrays by pointers
Array အား pointer ၿဖင့္ passing ၿပဳလုပ္ေသာ Function prototype ၏ syntax သည္ ေအာက္ပါအတိုင္းၿဖစ္သည္။
Return_type  function_name(type_array_element  *array_name)

Example
# include <iostream>
using namespace std;
# include <iomanip.h>
const int N = 10;
void Fn _ Name (int arr [], int n);                       / / Function works with the name of the array
void Fn _ Ptr (int * aPtr, int n);                         / / Function is a pointer to an array
void main ()
{ int mas [N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int * mPtr;
mPtr = mas;
for (int i = 0; i <N; i + +) cout << setw (4) << mas [i];
cout << endl;
Fn_Name (mas, N);
for (int i = 0; i <N; i + +) cout << setw (4) << mas [i];
cout << endl;
Fn_Ptr (mPtr, N);
for (int i = 0; i <N; i + +) cout << setw (4) << mas [i];
cout << endl;
}
void Fn_Name (int arr [], int n)
{  for (int cnt = 0; cnt <n; cnt + +)
arr [cnt] * = 2;
}
void Fn_Ptr (int * aPtr, int n)
{ for (int cnt = 0; cnt <n; cnt + +)
        * aPtr + + * = 3;         / / Equivalent to * (aPtr + +) * = 3;
                                          / / Increment have the same precedence and associativity right
                                 
}
It will be printed:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
6 12 1 8 24 30 36 42 48 54 60

String as function argument(Строки как аргументы функций)
String ဆိုသည္မွာလည္း character type , array element မ်ားၿဖစ္ေသာေၾကာင့္ function တြင္ ၎တို႕အား passing ၿပဳလုပ္ရာတြင္လည္း array element မ်ားအား အၿခား data type မ်ားၿဖင့္ passing ၿပဳလုပ္ၿခင္းၿဖင့္အတူတူပင္ၿဖစ္သည္။

Example.
# include <iostream>
using namespace std;
void Fn_Str (char *);
int main ()
{  char str [] = "this string we pass to the function";
    cout << str << endl;
    Fn_Str (str);
    cout << str << endl;
    return 0;
}
void Fn_Str (char * sPtr)
{ while (* sPtr)
{  if (* sPtr! = '') * sPtr - = ('a' - 'A');
          sPtr + +;
}
}
အထက္ေဖာ္ၿပပါ ပုစၦာတြင္ string အား upper case ေၿပာင္းလဲ၇န္အတြက္ ASCII – code အားအသံုးၿပဳၾကည့္ရွဳ႕နိဳင္သည္။၎တြင္ symbol တိုင္းသည္ တိက်ေသာ တန္ဖိုးရွိၿပီး ပုစၦာတြင္ေပးထားေသာ symbol မ်ားအား upper case အၿဖစ္ေၿပာင္းလဲေပးလိမ့္မည္ၿဖစ္သည္။
SPtr pointer သည္ str array ၏ address အား copy ယူသြားၿပီး pass ၿပဳလုပ္သြားမည္ၿဖစ္သည္။
အကယ္၍ symbol မ်ားသည္ ‘\0’ သို႕ blank နွင့္မတူညီလွ်င္ *sPtr ၏ တန္ဖိုးမ်ားသည္ ေၿပာင္းလဲသြားမည္ၿဖစ္သည္။

Passing of the structure by reference and by pointer(Передача структур по указателю и по ссылке)
Function မ်ားသည္ ၎တို႕၏ parameter မ်ားအား standard data type မ်ား၏ pointer မ်ားသာမက structure မ်ား၏ pointer အၿဖစ္ပါအသံုးၿပဳနိဳင္သည္။

Example.Passing structure by pointer.
#include<iostream>
#using namespace std;
#include<iomanip.h>
#include<conio.h>
typedef struct date {  int y;     //year;
          int m;    //month;
          int d;    //date;
             }Date;
void Printdate(Date *dPtr)
{ cout<<setw(4)<<dPtr->y<<’/’;
  if(dPtr->m<10)cout<<’0’;
  cout<<setw(1)<<dPtr->m<<’/’;
  if(dPtr->d<10)cout<<’0’;
 cout<<setw(1)<<dPtr->d<<endl;
}
int main()
{ Date birthday = { 1980,1,28};
   Printdate(& birthday);
getch();
return 0;
}
အထက္ေဖာ္ၿပပါပုစၦာတြင္ variable dPtr သည္ structure ၏ variable type အၿဖစ္သို႕ addressing ၿပဳလုပ္ထားၿခင္းၿဖစ္သည္။structrue သည္ pointer ဆီသို႕ addressing ၿပဳလုပ္ထားေသာေၾကာင့္ ၎ structure အတြင္းရွိ field-member မ်ားအား display (သို႕) aceess ၿပဳလုပ္လိုပါက operator “ -> “ အားအသံုးၿပဳရမည္ၿဖစ္သည္။
Function အား pointer passing ၿပဳလုပ္ၿခင္းသည္ stack ေပၚတြင္ structure အား byte copy မၿဖစ္ပြားေစတက္ေသာေၾကာင့္ ကြန္ပ်ဳတာ memory နွင့္ အခ်ိန္ၾကာၿမင့္မွဳ႕တို႕အား သက္သာေစသည္။

Example.Translation room size from meters to feet (passing parameter is by reference)
(အခန္း size အား meter မွ လက္မ အၿဖစ္သို႕ေၿပာင္းလဲေပးေသာ ပရိုဂမ္ (by reference နည္းၿဖင့္)
#include<iostream>
using namespace std;
#include<conio.h>
struct room { float w; //width
  float l; //length
  float h; //heigh
        };
Float m_to_Ft(room &);
Void print_room(room &);
int main()
{ room big_room={3.6,4.8,2.75};
 Room small_room={2.7,3.6,2.75};
clrscr();
float big_S,small_S;
print_room(big_room);
cout<<”The area of a large room”<<big_S<<”square feet”<<endl;
small_S =m_to_Ft(small_room);
cout<<”The area of a small room”<<small_S<<”square feet”<<endl;
print_room(big_room);
print_room(small_room);
return 0;
}
Float m_to_Ft(room & Room)
{ Room.w=Room.w*3.28;
 Room.l=Room.l*3.28;
Room.h=Room.h*3.28;
Return Room.w*Room.l;
}
Void print_room(room & Room)
{ cout<<Room.w<<”<<Room.l<<”<<Room.h<<endl;
}
အထက္ေဖာ္ၿပပါ ပုစၦာတြင္ m_to_Ft() ဖန္ရွင္သည္ ေပးထားေသာ အခန္း size အား meter ယူနစ္မွ feet ယူနစ္သို႕ေၿပာင္းလဲေပးၿပီး ထို႕အတူ အခန္း၏ ဧရိယာကိုလည္းတြက္ခ်က္ေပးသည္ကိုေတြ႕ၿမင္နိဳင္သည္။ reference နည္းၿဖင့္ passing ၿပဳလုပ္ေသာ တန္ဖိုးသည္ function အလုပ္လုပ္ေနေသာအခ်ိန္တြင္ ေၿပာင္းလဲသြားမည္ၿဖစ္ၿပီး ထိုေၿပာင္းလဲ၍ရလာေသာ တန္ဖိုးသည္ ဖန္ရွင္အလုပ္လုပ္ၿပီးဆံုးသြားေသာ အခ်ိန္တြင္ က်န္ရွိေနမည္ၿဖစ္သည္။

Reference as the return value of the link(Ссылка в качестве возвращаемого значения функции)
Reference အား function ၏ return value အၿဖစ္ return ၿပန္ရန္အသံုးၿပဳလိုေသာအခါ ေအာက္ေဖာ္ၿပပါ ဥပမာအတိုင္း အသံုးၿပဳနိဳင္သည္။

Example.Return value by reference.
#include<iostream>
using namespace std;
int x;
int & set(){return x;}
int main()
{ set()=92; //assign the value of x with function;
   //return a reference;
 cout<<”x=”<<x<<endl;
 return 0;
}

Function that return pointers(Функции, возвращающие указатели)
Function မ်ားသည္ pointer မ်ားအား ၎၏ argument မ်ားအၿဖစ္အသံုးၿပဳနိဳင္ယံုသာမက return ၿပန္ၿခင္းလည္းၿပဳလုပ္နိဳင္သည္။

Example.Returning from a function of pointer.
#include<oistream>
using namespace std;
#include<ctype.h>
#include<string.h>
char *UpperCase(char *s); //UpperCase() – function သည္ char_type pointer အား return //ၿပန္လိမ့္မည္ၿဖစ္သည္။
int main()
{  char title[]=”Upper the alphabet”;
    char *cPtr;
    cPtr = UpperCase(title);
    cout<<cPtr<<endl;
    return 0;
}
char *UpperCase(char *s)
{    int i;
     for(i=0;i<strlen(s);i++)
    s[i]=toupper(s[i]); //argument ၏ address အား return ၿပန္ရန္။
    return s;
}
Pointer အား function ၏ argument အၿဖစ္ passing ၿပဳလုပ္ေသာအခါ ၎ argument သည္ function အတြင္းတြင္ ေၿပာင္းလဲသြားမည္ၿဖစ္ၿပီး function အား calling ၿပဳလုပ္လိုက္ေသာအခါ ထိုေၿပာင္းလဲ လိုက္ေသာ တန္ဖိုးသည္ pass ၿဖစ္သြားမည္ၿဖစ္သည္။

                                                                                                 
translated by miet51




No comments:

Post a Comment