Basics
In C++, whenever a variable is declared, it is given a specific address
in memory. In some way, an array is similar in that the index of an array is a certain location of
a piece of data.
In general, a pointer is "pointing" to a certain address in memory. So let's see how to declare a
pointer in general:
Where in the above, type is the appropraite data type and * is the operator to denote that the variable
is a pointer. But what does with this is the following:
Where in this above block, there must be a REGULAR TYPE before there can be any pointer variables declared
or else the pointers would be pointing to nothing! An example will certainly help see this.
Example 1:
Sample pointer program
Download source code here (Right click - Save Tagret As...)
Here is what happens. First, two regular integer variables named x and y
are declared with initial values.
Second, two integer pointers are declared that will point to those variables. They are named
a and b. So far, nothing unusual has happened here.
Now, we are going to assign the address of the regular integers x and y into the pointers a and
b. This is done by using the single ampersand (&) BEFORE the regular integer variable. This
tells C++ to place the address of x into a, and the same goes for the pointer b, assign the address
of y.
Now that the addresses have been stored into the pointer variables, we can get the values of the
variables by what's called dereferencing the pointer. This is done
with the single (*) operator. DO NOT BE CONFUSED: The single star has multiple meanings when dealing
with pointers! (see below for summary chart).
Once they are dereferenced, the values of the regular variables are obtained and the output is
"0 5" on screen.
Pointers & Array's
You can also think of an array as a sequence of addresses in memory. Once you declare an array of
a fixed size, the program is setting aside N blocks of consecutive memory. So now that array's
are a collection of addresses, you can use them with pointers.
Here is how to declare a pointer to an array:
There is no need in the above to use the ampersand for giving the address of the whole array since we
now know that the array itself is a collection of addresses.
The above will declare the array called arr of type double and a double pointer p
that will point to the ENTIRE ARRAY. So now let's say you wanted to begin the pointer at a certain
spot in the array (you can do that of course). Here's how:
Note that a set of parenthesis has been added as well as a number. That number is the index of the
array; how sweet that is. So in general, to declare a pointer to an array, follow these two lines:
Wow! Seems like a lot but not really. Using the knowledge of array's we can skip explaining
the first line. But with the second line above, we have 2 choices; a pointer of a specific type
to the array name of that type (option 1). Or, a pointer of a specific type to a certain element
of an that array. Another example will certainly clarify this.
Example 2:
Sample array pointer program
Download source code here (Right click - Save Tagret As...)
Here is what happens. First, we declared an array of type double with initial values and
then declared a pointer to that array. We started the pointer at array[2] (literally).
After that, we see a set of cout statements with a comment representing the line number. Let's
observe each line.
Line 1
This will dereference the pointer p and give the value 7 because we started the pointer
at arr[2] -> (arr+2). This simply gets the value.
Line 2
This will cout the ADDRESS of the array arr. Another way of saying this is "the address
of the first element of the array" or "the address of the index 0 in the array". This address
will always be a hexadeciaml number. It will vary each time the program is run on a machine.
Line 3
This will dereference the address of (arr+3) or arr[3] and give the value 8 because of the value
in the array already.
Line 4
This will dereference the value of (arr+0) or arr[0] and give the value 5 since that is the value
of arr[0]. This could have been rewritten as *arr without the parenthesis.
Line 5
This will dereference the address of (arr+0) and ADD 9 TO THAT VALUE. This happens because there
are NO PARENTHESIS surrounding the expression. This could have been rewritten as
*(arr+0)+9.
sizeof() As A Function
The sizeof() is a built in function for C++. It will return to you an integer representing (IN BYTES)
the size of the parameter. Please see the chart in tutorial 2 regarding the size of the
data types in c++.
So now let's say we have the following snippet of code:
We will get 4 since ONE integer in C++ is 4 bytes. The same thing applies to arrays:
This will output "12" since there are 3 elements in the array, all of size 4.