Array and Pointer Tutorial

Discussion in 'C++' started by Tomás, May 11, 2006.

  1. Tomás

    Tomás Guest

    Some programmers treat arrays just like pointers (and some even think that
    they're exactly equivalent). I'm going to demonstrate the differences.

    Firstly, let's assume that we're working on a platform which has the
    following properties:

    1) char's are 8-Bit. ( "char" is synomonous with "byte" ).
    2) int's are 32-Bit. ( sizeof(int) == 4 ).
    3) Pointers are 64-Bit. ( sizeof(int*) == 8 ).


    First let's make two declarations:

    int main(void)
    {
    int array[5];

    int* const pointer = (int*)malloc( 5 * sizeof(int) );
    }


    Now I'll demonstrate how "array" and "pointer" are different:


    I'll start off with simple analgous expressions:

    ============================================================================
    | Expression | Type and Access Specifiers | That in English |
    ============================================================================
    | | | |
    | array | int[5] | An array of five int's.|
    | | | |
    |---------------------------------------------------------------------------
    | | |A const pointer which |
    | pointer | int* const |points to a modifiable |
    | | |int. |
    |--------------------------------------------------------------------------|
    | | |A const pointer which |
    | &array | int (* const)[5] |points to a modifiable |
    | | |array of five int's. |
    |--------------------------------------------------------------------------|
    | | |A const pointer, which |
    | &pointer | int* const* const |points to a const |
    | | |pointer, which points to|
    | | |a modifiable int. |
    ============================================================================


    Here's how "sizeof" works with them:


    ===========================================================
    | Expression | sizeof( exp ) | But Why? |
    ===========================================================
    | | | |
    | array | 20 | It's five int's. |
    | | (5 * 4) | |
    |---------------------------------------------------------|
    | | | |
    | pointer | 8 | It's just a pointer.|
    | | (just 8) | |
    |---------------------------------------------------------|
    | | | |
    | &array | 8 | It's just a pointer.|
    | | (just 8) | |
    |----------------------------------------------------------
    | | | |
    | &pointer | 8 | It's just a pointer.|
    | | | |
    | | (just 8) | |
    ===========================================================


    Okay next thing to discuss is the usage of square brackets, and the
    dereference operator. The two of these are to be used by pointers only. So
    how come we can use them with arrays, as follows?:

    array[0] = 4;

    *array = 6;

    The reason is that an expression of the following type:

    int[5]

    can implicitly convert to an expression of the following type:

    int* const

    What it does is convert to a pointer to the first element of the array.
    Therefore, the first example:

    array[0] = 4;

    implicitly converts "array" to a normal pointer, then uses chain brackets to
    access memory at a certain offset from the original address.

    Also the second example:

    *array = 6;

    implicitly converts "array" to a normal pointer, then dereferences it.


    NOTE: You must remember that an array implicitly converts to a pointer to
    its first element, NOT to a pointer to the array. This fact has a few
    implications. Here's one such implication:


    *(array + 3) = 6;


    What the above line of code does is the following:

    1) Implicitly converts "array" to an: int* const
    2) Adds 3 * sizeof(int) to the address.
    3) Dereferences the resulting pointer, and assigns 6 to it.


    If "array" implicitly converted to: int (*)[5]
    rather than a pointer to the first element, then Step 2 above would be
    different, specifically:


    2) Adds 3 * sizeof( int[5] ) to the address.


    And we know that sizeof(int[5]) is 20 on this platform (not 8!).


    So you may ask, "What's the point in having a pointer to an array?" -- well
    here's where it may come in handy:


    void SomeFunc ( int (* const p_array)[5] )
    {
    (*p_array)[0] = 99;
    (*p_array)[1] = 98;
    (*p_array)[2] = 97;
    (*p_array)[3] = 96;
    (*p_array)[4] = 95;

    /* This function won't accept an array of any
    other size! */
    }


    And here's a C++-specific example with references:

    void SomeFunc ( int (&array)[5] )
    {
    array[0] = 99;
    array[1] = 98;
    array[2] = 97;
    array[3] = 96;
    array[4] = 95;

    /* This function won't accept an array of any
    other size! */
    }


    Also in C++, you can exploit the use of templates:

    template<class T, unsigned long len>
    void KeepCopy( const T (&array)[len] )
    {
    static my_array[len];

    /* Do some other stuff */
    }


    I've posted this to a few newsgroups, so if you'd like to reply, please post
    to comp.lang.c because it's the common denominator. If your post is C++-
    specific, the please post to comp.lang.c++.

    Did I leave anything out?

    -Tomás

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    --
    comp.lang.c.moderated - moderation address: -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.
     
    Tomás, May 11, 2006
    #1
    1. Advertising

  2. Tomás wrote:

    > Some programmers treat arrays just like pointers (and some even think that
    > they're exactly equivalent). I'm going to demonstrate the differences.


    Some of these days you can also write an article demonstrating that integer
    types are not floating point types. It will be another great contribution
    to the progress of humanity.

    --
    Salu2

    Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, May 11, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    349
    Tim Rentsch
    Oct 9, 2004
  2. Tomás

    Array and Pointer Tutorial

    Tomás, May 11, 2006, in forum: C Programming
    Replies:
    53
    Views:
    1,038
    Richard Bos
    May 22, 2006
  3. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    676
    Martin Ambuhl
    Jan 28, 2008
  4. RSL
    Replies:
    14
    Views:
    3,266
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    457
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page