NOTE:

These are a little sketchy...I'll fill in details 'soon'.

Pointers and Functions

Using Pointer Arguments

Another operator

Pointers & Arrays

as it turns out, an array declared such as:

    type arr[CONST_SIZE];

is actually a pointer which must point to a particular memory location (the start of the array set aside by the compiler)

  arr
    +----+                                  +==+==+==+
    |  --|--------------------------------->|  |  |  |
    +----+                                  +==+==+==+

so you've actually been using pointers for quite some time and just didn't know it!

Is there a difference at all? Well, the array is actually a constant pointer rather than a plain pointer. That is, it has been initialized to point to the sequence of data and cannot be made to point anywhere else — the pointer itself is const and can point to no other address.

If you ever have need, you can do this yourself. It looks a bit strange at first, but you get used to it:

    type * const ptr = &object;         // ptr will always point to object

The reason that the const keyword must follow the * in the declaration is that a const to the left of the * would modify the base type of the pointer! At some point, someone thought it a good idea to have both type const and const type mean the same thing — and others backed them up at a standards committee! *sigh* *shakes head* *shrug* Wha'cha gonna' do?

So, that means that both of these:

    const type * ptr1;
    type const * ptr2;

make the pointers point to a value they believe to be constant. Likewise, both of these:

    const type * const ptr3 = &object;
    type const * const ptr4 = ptr;

make pointers which constantly point to values they believe to be unchangeable.

In summary, there are four ways you can apply const-ness to a pointer:

Declaration(s)Meaning
    type * ptr1;
both the pointer's destination and the value at that address can be altered
    const type * ptr2a;
    type const * ptr2b;
the pointer's destination can be altered, but the value at that address can NOT be altered
    type * const ptr3 = &object;
the pointer's destination can NOT be altered, but the value at that address can be altered
    const type * const ptr4a = &object;
    type const * const ptr4b = ptr2a;
neither pointer's destination nor the value at that address can be altered

Pointer Arguments and Pointer Math

Pointers and C-strings

Watch for the Weirdness