Pointers and Reference n Pointers n p p
- Slides: 20
ตวชและการอางอง (Pointers and Reference) n (Pointers) ตวช n ตวแปร p ถกเรยกวาตวช คาทเกบใน คอเลขทอยของหนวยความจำของตวแปร p n int n = 33 ; int* p; p = &n ; cout << p ; p 0 x 3 fffd 18 0 x 3 fffd 14 int n 33 int p 0 x 3 fffd 14 33 n 25 June 2006 12. 00 AM Week 05 a - Pointers and Reference int 6
ตวชและการอางอง (Pointers and Reference) 0 x 3 fffd 20 0 x 3 fffc 28 y x 145 int cout << y ; คำตอบทไดคอ cout << &y ; คำตอบทไดคอ cout << *y ; คำตอบทไดคอ 25 June 2006 12. 00 AM Week 05 a - Pointers and Reference 0 x 3 fffc 28 0 x 3 fffd 20 145 8
ฟงกชน การสงผานตวชใหกบพารามเตอรของฟงกชน ต. ย. // pass by value void my. Function( int ); // pass by reference void my. Function( int* ); main( ) { int n = 4 ; int* p= &n ; my. Function( *p ) ; cout << n ; } main( ) { int n = 4 ; int* p= &n ; my. Function( p ) ; cout << n ; } void my. Function( int ptr) { ptr = ptr + 10 ; } void my. Function( int* ptr) { *ptr = *ptr + 10 ; } 25 June 2006 12. 00 AM Week 05 a - Pointers and Reference 10
(Dealloc การคนเนอทใหหนวยความจำ ate) ตวดำเนนการ delete ใชในการคน memory ตำแหนงท pointer ชไปใหกบระบบ int* p = new int( ); *p = 33 ; delete p ; p 33 new int() *p = 44 ; // Error : p has been deallocated p 25 June 2006 12. 00 AM ? … Dangling pointer Week 05 a - Pointers and Reference 13
ปญหาเนอทขยะ (Garbage memory) int* p = new int( ); int* r = new int( ); *p = 33 ; *r = 44; p =r; ตวอย าง 25 June 2006 12. 00 AM // not error but garbage memory Garbage Memory p 33 r 44 Week 05 a - Pointers and Reference 14
( array of pointers) อารเรยของตวช การประกาศ Array สมาชกของอารเรยอาจเปนตวชกได ของ Pointer เขยนไดเปน data_type* array_name[size] int* p[4] ; p[0] = new int(22); p[1] = new int(33); p[1] = new int(44); p[1] = new int(55); p P[0] 22 33 P[1] P[2] 44 P[3] 55 25 June 2006 12. 00 AM Week 05 a - Pointers and Reference 17
โครงสรางและตวช ตวชทชไปทโครงสราง ( pointer to structure ) n มการใชตวดำเนนการ -> แทนตวชทชไปยงสมาชกของโครงสราง #include <iostream> #include <string> using namespace std; s 1 struct Student { string name; int age; float score; }; name = Somjai age = main() { Student s 1; s 1. name = “Somjai” ; s 1. age = 18 ; s 1. score = 2. 85 f ; } 25 June 2006 12. 00 AM Week 05 a - Pointers and Reference 18 score = 2. 85 f 18
โครงสรางและตวช main() { Student s 1; s 1. name = “Somjai” ; s 1. age = 18 ; s 1. score = 2. 85 f ; Student* s 2; s 2 = &s 1; } s 2 s 1 name = Somjai Somsri (*s 2). name = “Somsri” ; s 2 -> age = 17 ; s 2 -> score = 2. 90 f ; age = 18 17 score = 2. 85 f 2. 90 f 25 June 2006 12. 00 AM Week 05 a - Pointers and Reference 19
- Reference node and non reference node
- Reference node and non reference node
- Pointers and strings
- Java pointers and references
- & vs * in c
- Advantages of pointers
- Significance of pointer
- C array of pointers to structs
- Pointers basics
- Which is a good idea for using skip pointers
- Skip pointers in information retrieval
- Which is a good idea for using skip pointers
- Explicit pointers
- Pointers in java
- Inter function communication
- Glsl pointers
- Shuffle left algorithm
- Hazard pointers
- 12 pointers
- I-need-a-few-pointers-98hpou5
- Science logbook example