Can anyone please help me with understanding double and triple pointers?
Why do we require double and triple pointers?
Collapse
X
-
Tags: None
-
Check this out:
The variable x is an int.Code:int x;
The variable x is an int, the variable y is an int.Code:int x, y;
The variable x is an int, the variable y is an int, the variable *z 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, the variable **p 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, the variable *** q is an int.Code:int x, y, *z, **p, ***q;
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*
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**.Code:char* poem[5]; //a poem of five pointers
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**:
To add the poem to the book, remember the name book is the address of book[0]. Therefore, *book is book[0]. Therefore:Code:char** book[5]; //a book of 5 poems.
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).Code:*book = poem; //add poem ADDRESS to book[0].
This can be expanded to a array of poetry books:
I will leave the rest to you to access a line of the poem using the bookset array.Code:char*** bookset[5]; //array of 5 books.
-
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
-
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
Comment