Why do we require double and triple pointers?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KushShukla
    New Member
    • Jan 2014
    • 11

    #1

    Why do we require double and triple pointers?

    Can anyone please help me with understanding double and triple pointers?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Check this out:

    Code:
    int x;
    The variable x is an int.

    Code:
    int x, y;
    The variable x is an int, the variable y is an int.

    Code:
    int x, y, *z;
    The variable x is an int, the variable y is an int, the variable *z is an int.

    Code:
    int x, y, *z, **p;
    The variable x is an int, the variable y is an int, the variable *z is an int, the variable **p is an int.

    Code:
    int x, y, *z, **p, ***q;
    The variable x is an int, the variable y is an int, the variable *z is an int, the variable **p is an int, the variable *** q is an int.

    So far so good?

    OK. Let's take the variable z. In order for *z to be an int, the variable z itself ,must contain the address of that int. z is called a pointer. The * is called the dereference operator. So, you put the address of the int in z, then code *z to tell the compiler to go to the address in z and use the int at that address. Hence, *z is an int.

    Let's suppose you have a poem in an array. Each line of a poem is a string expressed as a char array. Since a char array for a line is seen as a char* (remember the name of an array is the address of element 0), the poem becomes an array of pointers to char, or char*

    Code:
    char*  poem[5];   //a poem of five pointers
    Here is the important thing: The name of an array is the address of element 0. Therefore, the name "poem" is the address of a char*. Therefore, poem is a char**.

    Hence, *poem is a char*. Hence *poem is line 1 of the poem. Hence *(poem +1) is line 2 of the poem, *(poem +2) is line 3 of the poem, etc...

    If the poem is put into a book of poems you have an array of pointers-to-pointers-to-char, or char**:

    Code:
    char** book[5];    //a book of 5 poems.
    To add the poem to the book, remember the name book is the address of book[0]. Therefore, *book is book[0]. Therefore:

    Code:
    *book = poem;  //add poem ADDRESS to book[0].
    You can use the book array to display a line of the poem. Since book is the address of element 0, then *book is element 0 (the poem) and **book is line 1 of the poem.. The ADDRESS of second line of the poem would be *book +1 making the second line itself *(*book + 1).

    This can be expanded to a array of poetry books:

    Code:
    char*** bookset[5];    //array of 5 books.
    I will leave the rest to you to access a line of the poem using the bookset array.
    Last edited by weaknessforcats; Jan 28 '14, 12:43 AM. Reason: fix code tags

    Comment

    • KushShukla
      New Member
      • Jan 2014
      • 11

      #3
      bookset is the address of element 0,
      *bookset = 1st book,
      **bookset = 1st poem
      ***bookset = 1st line of the poem

      Hence, **(*bookset+1) = 2nd line of the poem.
      and so on....

      Have I got right?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Looks OK by eyeball.

        Here's some starter code. Run this through the compiler and see what happens.

        Code:
        int main()
        {
         char line1[] = "Mary had a little lamb";
         char line2[] = "it's fleece was whte as snow";
         char line3[] = "She took it to Pittsburgh";
         char line4[] = "And now look at the poor thing.";
        
         //Put the lines in a poem
        
         char* poem[4];     //array of 4 char pointers
         *poem=      line1;
         *(poem+1) = line2;
        *( poem +2) = line3;
         *(poem +3) = line4;
        
         //put the poem in a book
        
         char** book[5];    //an array 
        
         *book = poem;
        
         cout << **book << endl;
         cout << *(*book + 1) << endl;
        }

        Comment

        Working...