ผลต่างระหว่างรุ่นของ "Adt lab/pointers"
Jittat (คุย | มีส่วนร่วม) |
Jittat (คุย | มีส่วนร่วม) |
||
แถว 143: | แถว 143: | ||
== Pointer arithmetics == | == Pointer arithmetics == | ||
+ | You can do arithmetic operations with pointers. Try to figure out how it works with this example. | ||
+ | |||
== Pointers and arrays == | == Pointers and arrays == | ||
== Other links == | == Other links == |
รุ่นแก้ไขเมื่อ 22:45, 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. These locations are also referred to as memory "addresses". 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;
Let's look at each step.
p = &a; // step 1
To obtain a location of any variable, we use operator & (called a reference operator). After step 1, p keeps the location of a.
*p = 100; // step 2
To dereference a pointer variable, we use operator *. Therefore *p refers to the "data" at the location that p points to. After step 2, *p (which is essentially a) becomes 100.
a++; // step 3
As p points to a's location, if we change the value of a, *p also changes (because it is the "same" piece of data).
p = &b; // step 4
a = *p;
We can change p to point to other places.
Can we see the value of the pointer?
We can add the following line into the program after each pointer assignment.
cout << "p: " << p << endl;
This is the output.
p: 0x7ffd9329b338 10 100 101 p: 0x7ffd9329b33c 20
Note that the locations 0x7ffd9329b338 and 0x7ffd9329b33c in your machine and each program execution may be different. These depend on where the program is loaded into the memory and how the memory allocation actually works.
Passing parameters by reference
In C, function parameters are passed by value only. Therefore, if you want to write function swap that swaps two variables, you cannot do it like this:
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
// ...
int x = 10; int y = 100;
swap(x,y);
// ... nothing changes here ...
The reason for that is that the code only modifies "local" copies a and b of x and y.
Pointers come to rescue.
If you can only pass-by-value, the only way a function can modify variables outside its scope, it to pass their address to the function.
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// ...
int x = 10; int y = 100;
swap(&x,&y);
// ... now x = 100, y = 10
As a quick rule, if you want a function to change a variable, pass it as a pointer, then use it "indirectly" through the pointer.
Reference types
To make our life better, C++ introduces reference types. Function swap can be written like this.
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
// ...
int x = 10; int y = 100;
swap(x,y)
// .. now it works!: x = 100, y = 10
Behind the scene, the implementation is pretty much like the pointer version, but it is done automatically by the compiler.
Pointer arithmetics
You can do arithmetic operations with pointers. Try to figure out how it works with this example.