Welcome guest! Click here to register or login.
logo

<< Tutorial 10
Tutorial 12 >>

Pointers in C++

***Please register FREE to rate this tutorial***


Topics
Basics
Pointers and Arrays
sizeof() As A Function
Examples
Example 1
Example 2

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:

type *var1, *var2, ... , *varN;

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:

type var1, var2, ... , varN;
type *pVar1, *pVar2, ... , pVarN;

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...)

#include <iostream>
using namespace std;

int main(){

//regular int variables:
int x=0, y=5;

//pointers to those variables:
int *a, *b;

//assign the addresses:
a = &x;
b = &y;

//dereference the pointers to get the values:
cout << *a << "  " << *b << endl;

return 0;
}

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:

double arr[4] = {5.0, 6.0, 7.0, 8.0};

double *p = arr;

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:

double arr[4] = {5.0, 6.0, 7.0, 8.0};

double *p = (arr+2);

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:

type arrayName[ size ] = initial_value(s);

type *pVarName = arrayName; OR BELOW:
type *pVarName = (arrayName+index);

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...)

#include <iostream>
using namespace std;

int main(){

double arr[] = {5.0, 6.0, 7.0, 8.0};

double *p = (arr+2);

cout << *p << endl;   // 1
cout << arr << endl;    // 2
cout << *(arr+3) << endl;  // 3
cout << *(arr) << endl;  // 4
cout << *arr+9 << endl;  // 5

return 0;
}

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:

#include <iostream>
using namepsace std;

int main(){

int x = 9;
int* p = &x;

cout << sizeof(p) << endl;

return 0;
}

We will get 4 since ONE integer in C++ is 4 bytes. The same thing applies to arrays:

#include <iostream>
using namepsace std;

int main(){

int x[] = {9,8,7};

cout << sizeof(x) << endl;

return 0;
}

This will output "12" since there are 3 elements in the array, all of size 4.


<< Tutorial 10
Tutorial 12 >>