ผลต่างระหว่างรุ่นของ "Adt lab/pointers"
ไปยังการนำทาง
ไปยังการค้นหา
Jittat (คุย | มีส่วนร่วม) |
Jittat (คุย | มีส่วนร่วม) |
||
แถว 13: | แถว 13: | ||
</source> | </source> | ||
+ | To see how pointers work, let's follow this code. | ||
− | + | <source lang="cpp"> | |
+ | int a = 10; | ||
+ | int b = 20; | ||
+ | p = &a; | ||
+ | cout << (*p) << endl; | ||
+ | |||
+ | *p = 100; | ||
+ | cout << a << endl; | ||
+ | |||
+ | a++; | ||
+ | cout << (*p) << endl; | ||
+ | |||
+ | p = &b; | ||
+ | a = *p; | ||
+ | cout << a << endl; | ||
+ | </source> | ||
== Pointers and arrays == | == Pointers and arrays == | ||
== Other links == | == Other links == |
รุ่นแก้ไขเมื่อ 15:43, 27 สิงหาคม 2558
- This is part of adt lab.
Pointers
In C/C++, there is a special kind of types: pointers. Pointer variables keep locations in the memory. To declare a pointer variable, we use symbol *:
type* variable;
For example, the following code declares p as a pointer to an integer.
int* p;
To see how pointers work, let's follow this code.
int a = 10;
int b = 20;
p = &a;
cout << (*p) << endl;
*p = 100;
cout << a << endl;
a++;
cout << (*p) << endl;
p = &b;
a = *p;
cout << a << endl;