Pointers in C++ operate in precisely the same way as pointers in C. To declare a pointer, you
must state the type of the object that the pointer will point to, e.g. to declare a variable i as
being a pointer to int:
int *i;
The pointer is then used in one of two ways: by making the pointer point to some already
existing data; or by making the pointer point to some currently unused memory and then
storing data via the pointer. Examples of both methods are given below:
int i = 10;
int *ptr2int;
ptr2int = &i; // make ptr2int point to i
*ptr2int = 20; // change the value of i
ptr2int = new int; // ask for enough memory to
// store an integer
cin >> *ptr2int; // store data in this newly
// allocated memory
The * and & operators are more or less the opposites of each other. & is used to generate a
pointer, and * is used to follow a pointer to find what is at the other end. The opposite
nature of the two is shown by the fact that *&i is the same as i (given the declarations
above), but &*i is not legal as you are attempting to treat i as a pointer.
References are ways of introducing a new name for an existing object. References must be
initialised to refer to some existing object, and this cannot be changed! For example,
consider the code below:
int i = 1, j = 2;
int& k = i; // k refers to i, i.e. k
// is an alias for i
cout << setw(2) << i << j << k; // prints " 1 2 1"
k = j; // assigns 2 to i
cout << setw(2) << i << j << k; // prints " 2 2 2"
Note that the assignment to k did not make k refer to j, instead it modified the value of the
object that k referred to.
must state the type of the object that the pointer will point to, e.g. to declare a variable i as
being a pointer to int:
int *i;
The pointer is then used in one of two ways: by making the pointer point to some already
existing data; or by making the pointer point to some currently unused memory and then
storing data via the pointer. Examples of both methods are given below:
int i = 10;
int *ptr2int;
ptr2int = &i; // make ptr2int point to i
*ptr2int = 20; // change the value of i
ptr2int = new int; // ask for enough memory to
// store an integer
cin >> *ptr2int; // store data in this newly
// allocated memory
The * and & operators are more or less the opposites of each other. & is used to generate a
pointer, and * is used to follow a pointer to find what is at the other end. The opposite
nature of the two is shown by the fact that *&i is the same as i (given the declarations
above), but &*i is not legal as you are attempting to treat i as a pointer.
References are ways of introducing a new name for an existing object. References must be
initialised to refer to some existing object, and this cannot be changed! For example,
consider the code below:
int i = 1, j = 2;
int& k = i; // k refers to i, i.e. k
// is an alias for i
cout << setw(2) << i << j << k; // prints " 1 2 1"
k = j; // assigns 2 to i
cout << setw(2) << i << j << k; // prints " 2 2 2"
Note that the assignment to k did not make k refer to j, instead it modified the value of the
object that k referred to.
No comments:
Post a Comment