C++ ညွှန်ပြချက်များ
Pointers ဖန်တီးခြင်း။
အော်ပရေတာ အသုံးပြုခြင်းဖြင့် variable တစ်ခု၏ memory address ကို ရနိုင်သည်ဟု ယခင်အခန်းမှ သင်သင်ယူခဲ့သည် -&
ဥပမာ
string food = "Pizza";
// A food variable of type string
cout <<
food; // Outputs the value of food (Pizza)
cout << &food; // Outputs the memory address of food (0x6dfed4)
သို့သော်လည်း pointer သည် memory address ကို ၎င်း၏တန်ဖိုးအဖြစ် သိမ်းဆည်း သည့် variable တစ်ခုဖြစ်သည် ။
pointer variable သည် အမျိုးအစားတူ ဒေတာအမျိုးအစား (ကဲ့သို့ int
သို့မဟုတ် ) ကိုညွှန်ပြပြီး အော်ပရေတာ string
ဖြင့် ဖန်တီးထားသည် ။ *
သင်လုပ်ဆောင်နေသော variable ၏လိပ်စာကို pointer တွင် သတ်မှတ်ထားသည်-
ဥပမာ
string food = "Pizza"; // A food variable
of type string
string* ptr =
&food; // A pointer variable, with the name
ptr, that stores the
address of food
// Output the value of food (Pizza)
cout << food << "\n";
// Output
the memory address of food (0x6dfed4)
cout << &food << "\n";
//
Output the memory address of food with the pointer (0x6dfed4)
cout <<
ptr << "\n";
နမူနာရှင်းပြထားပါတယ်။
ခရေပွင့်သင်္ကေတ ( ) ကိုအသုံးပြု၍ ကိန်း
ရှင် တစ်ခုသို့ ညွှန်ptr
ပြသောအမည် ဖြင့် pointer variable တစ်ခုကိုဖန်တီးပါ ။ ညွှန်ပြသည့်အမျိုးအစားသည် သင်လုပ်ဆောင်နေသော ကိန်းရှင်အမျိုးအစားနှင့် ကိုက်ညီရမည်ကို သတိပြုပါ။string
*
string* ptr
&
ခေါ်သော variable ၏ မှတ်ဉာဏ်လိပ်စာကို သိမ်းဆည်းရန် အော်ပရေတာအား အသုံးပြုကာ food
၎င်းကို ညွှန်ပြသူထံ သတ်မှတ်ပေးသည်။
ယခု၊ 's memory address ၏ ptr
တန်ဖိုးကို ရရှိထားသည် ။food
အကြံပြုချက်- pointer variable များကိုကြေငြာရန် နည်းလမ်းသုံးမျိုးရှိသည်၊ သို့သော် ပထမနည်းလမ်းကို ဦးစားပေးသည်-
string* mystring; // Preferred
string *mystring;
string * mystring;