Coding & programming
Installing CodeBlocs compiler
C language
C++ language
© The scientific sentence. 1998-2016
| Â |
|
Programming with C/C++ Language
Pointers, Arrays and Strings
Pointers in C++
1. Pointers
Let's see the following set of boxes, a kind of momory block
in the CPU of a computer:
2. What is a pointer ?
If the variable x has an address p = &x , we'll then be
able to go to that address and retrieve the data 17 stored in it.
We store a memory address &x = 246 in a pointer P .
The word pointer can refer either to a memory address &x itself,
or to a variable P that stores a memory address.
Usually, the distinction isn't really that important: if you pass a
pointer variable into a function, you're passing the value stored in
the pointer , that is the memory address.
The variable that stores a memory address is a pointer.
3. C++ Pointer Syntax
When we deal with a pointer, we need to request both the memory
location (P = &x = 246) and the value stored at that memory location (*P = x = 17).
When we declare a pointer variable, we tell the compiler that
the variable is a pointer, and what type of memory it points to. In
our example, the type is int.
The pointer declaration looks like this:
<variable_type> *<name>;
For example, we could declare a pointer that stores the address of an integer
with the following syntax:
int *points_to_integer;
Notice the use of the *. This is the key to declaring a pointer; if we add
it directly before the variable name, it will declare the variable to be a pointer.
If we declare multiple pointers on the same line, we must precede each of them
with an asterisk:
// one pointer, one regular int
int *pointer, nonpointer;
// two pointers
int *pointer1, *pointer2;
Pointers point to locations in memory.
As we said, there are two ways to use the pointer to access information:
it is possible to have it give the actual address to another variable. To do so,
simply use the name of the pointer without the *. However, to access the
actual memory location and the value stored there, use the *.
The technical name, for accessing the
actual memory location to retrieve the value stored there, is
dereferencing the pointer. In fact, we are taking the reference
to some memory address and following it, to retrieve the actual value.
int *p; would define a pointer to an integer, and *p would
dereference that pointer, meaning that it would actually retrieve
the data that p points to.
4. Value of a pointer
The value of a pointer is an address.
The use of a pointer is to store a memory address; so when we use the pointer:
call_to_function_expecting_memory_address(pointer);
then it evaluates to the address.
We have to add an asterisk, in order to retrieve the value stored at the address.
The pointer itself is supposed to store an address, so when we use the bare
pointer, we get that address back.
Note that it is a good practice to always initialize
pointers before using them.
5. Why pointers ?
If you happen to have a huge piece of data that you want to pass
into a function, it's a lot easier to pass its location to the
function than to copy every element of the data!
Pointers are a powerful programming tool. They can make some
tasks much easier and help improve our program's efficiency, and even allow
us to handle unlimited amounts of data.
It is also possible to use pointers to dynamically allocate memory,
which means that you can write programs that can handle nearly unlimited amounts of data.
|
  |