473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Why do we require double and triple pointers?

Can anyone please help me with understanding double and triple pointers?
Jan 27 '14 #1

✓ answered by weaknessforcats

Check this out:

Expand|Select|Wrap|Line Numbers
  1. int x;
The variable x is an int.

Expand|Select|Wrap|Line Numbers
  1. int x, y;
The variable x is an int, the variable y is an int.

Expand|Select|Wrap|Line Numbers
  1. int x, y, *z;
The variable x is an int, the variable y is an int, the variable *z is an int.

Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. 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*

Expand|Select|Wrap|Line Numbers
  1. 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**:

Expand|Select|Wrap|Line Numbers
  1. char** book[5];    //a book of 5 poems.
  2.  
To add the poem to the book, remember the name book is the address of book[0]. Therefore, *book is book[0]. Therefore:

Expand|Select|Wrap|Line Numbers
  1. *book = poem;  //add poem ADDRESS to book[0].
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. char*** bookset[5];    //array of 5 books.
  2.  
I will leave the rest to you to access a line of the poem using the bookset array.

3 6430
weaknessforcats
9,208 Expert Mod 8TB
Check this out:

Expand|Select|Wrap|Line Numbers
  1. int x;
The variable x is an int.

Expand|Select|Wrap|Line Numbers
  1. int x, y;
The variable x is an int, the variable y is an int.

Expand|Select|Wrap|Line Numbers
  1. int x, y, *z;
The variable x is an int, the variable y is an int, the variable *z is an int.

Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. 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*

Expand|Select|Wrap|Line Numbers
  1. 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**:

Expand|Select|Wrap|Line Numbers
  1. char** book[5];    //a book of 5 poems.
  2.  
To add the poem to the book, remember the name book is the address of book[0]. Therefore, *book is book[0]. Therefore:

Expand|Select|Wrap|Line Numbers
  1. *book = poem;  //add poem ADDRESS to book[0].
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. char*** bookset[5];    //array of 5 books.
  2.  
I will leave the rest to you to access a line of the poem using the bookset array.
Jan 28 '14 #2
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?
Jan 28 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
Looks OK by eyeball.

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

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.  char line1[] = "Mary had a little lamb";
  4.  char line2[] = "it's fleece was whte as snow";
  5.  char line3[] = "She took it to Pittsburgh";
  6.  char line4[] = "And now look at the poor thing.";
  7.  
  8.  //Put the lines in a poem
  9.  
  10.  char* poem[4];     //array of 4 char pointers
  11.  *poem=      line1;
  12.  *(poem+1) = line2;
  13. *( poem +2) = line3;
  14.  *(poem +3) = line4;
  15.  
  16.  //put the poem in a book
  17.  
  18.  char** book[5];    //an array 
  19.  
  20.  *book = poem;
  21.  
  22.  cout << **book << endl;
  23.  cout << *(*book + 1) << endl;
  24. }
Jan 28 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: S Manohar | last post by:
I need to pass a 'reference' to a double value to a function which changes that value. Each time the function is called, it needs to change different sets of values. In C I'd simply pass...
7
by: Brian van den Broek | last post by:
Hi all, I'm posting partly so my problem and solution might be more easily found by google, and partly out of mere curiosity. I've just spent a frustrating bit of time figuring out why pydoc...
11
by: Gregory L. Hansen | last post by:
I'm playing with a simulation where different parts of a system, like a controller or a thermometer or a thermal mass, are represented as classes, instantiated, and connected together like...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
1
by: Jeffers | last post by:
Hi, Can anyone give me an example code fragment to set one of these up? Thanks, Jeff.
1
by: Rain | last post by:
Hello Gurus! I really need this one to finish a module in my thesis. PLease please help me. I need a double buffer class in C# so i can call it and use it anytime i want, the problem is...
1
by: Little | last post by:
Could someone help me figure out how to put my project together. I can't get my mind wrapped around the creation of the 4 double Linked Lists. Thank your for your insight. 1. Create 4 double...
1
by: Little | last post by:
Hello everyone. I am trying to do the following program and am unable to get the beginning portion to work correctly. The scanner works when I print the statements without the double linked list...
3
by: livesinabox | last post by:
I am a student and I am having trouble understanding Dynamic Memory Allocation and the usage of Double Pointers. int **A A=(int **)malloc(r1*(sizeof(int*))); for(i=0;i<r1;i++) A=(int...
2
by: jeyshree | last post by:
please explain me the size of int ,float.,char,double,and pointers in 16 and 32 bit
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.