Connecting Tech Pros Worldwide Forums | Help | Site Map

Extracting Data from a Triple/Double Pointer pointed to a Multidimensional array

Newbie
 
Join Date: Sep 2007
Location: India
Posts: 2
#1: Sep 25 '07
Hi !
Please have a look at the code snippets.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. char arr[10] = {"abcdefghi"};
  4.  
  5. char **Dpp;
  6. Dpp = &ptr;
  7. printf("\n\nThis is test 2\n %s\n ",*Dpp);
  8. }
This is working fine.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. char arr[3][10] = {"abcdefghi","asdrfe","aserde"};
  4.  
  5. char **Dpp;
  6. Dpp = &ptr;
  7. printf("\n\nThis is test 2\n %s\n ",*Dpp);
  8. }
  9.  
The Second piece of code is failing.

Now the Question is
How to manipulate a Multidimensional Char array using a Double Pointer.
Also
I would like to know the Logic and Usage of multi level pointers
so that i can learn more

It would be of great help to me if you could helpme in resolving this issue

Thanks in Advance .

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#2: Sep 25 '07

re: Extracting Data from a Triple/Double Pointer pointed to a Multidimensional array


Quote:

Originally Posted by 16sport

Hi !
Please have a look at the code snippets.

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. char arr[10] = {"abcdefghi"};
  4.  
  5. char **Dpp;
  6. Dpp = &ptr;
  7. printf("\n\nThis is test 2\n %s\n ",*Dpp);
  8. }
  9.  
This is working fine.

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. char arr[3][10] = {"abcdefghi","asdrfe","aserde"};
  4.  
  5. char **Dpp;
  6. Dpp = &ptr;
  7. printf("\n\nThis is test 2\n %s\n ",*Dpp);
  8. }
  9.  
The Second piece of code is failing.

Now the Question is
How to manipulate a Multidimensional Char array using a Double Pointer.
Also
I would like to know the Logic and Usage of multi level pointers
so that i can learn more

It would be of great help to me if you could helpme in resolving this issue

Thanks in Advance .

Use Code Tags.
What is ptr, where it defined?
Please let me know :-)

Kind regards,
Dmjpro.
ilikepython's Avatar
Expert
 
Join Date: Feb 2007
Posts: 839
#3: Sep 25 '07

re: Extracting Data from a Triple/Double Pointer pointed to a Multidimensional array


Quote:

Originally Posted by 16sport

Hi !
Please have a look at the code snippets.

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. char arr[10] = {"abcdefghi"};
  4.  
  5. char **Dpp;
  6. Dpp = &ptr;
  7. printf("\n\nThis is test 2\n %s\n ",*Dpp);
  8. }
This is working fine.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. char arr[3][10] = {"abcdefghi","asdrfe","aserde"};
  4.  
  5. char **Dpp;
  6. Dpp = &ptr;
  7. printf("\n\nThis is test 2\n %s\n ",*Dpp);
  8. }
  9.  
The Second piece of code is failing.

Now the Question is
How to manipulate a Multidimensional Char array using a Double Pointer.
Also
I would like to know the Logic and Usage of multi level pointers
so that i can learn more

It would be of great help to me if you could helpme in resolving this issue

Thanks in Advance .

Basically, I think that the double (pointer to pointer) pointer is the same as 2D array since the name of the array is a pointer to a char pointer and that's exactly what the double pointer is. If you do:
Expand|Select|Wrap|Line Numbers
  1. Dpp = arr;
  2.  
I think you should be able to use Dpp as you use arr.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,382
#4: Sep 25 '07

re: Extracting Data from a Triple/Double Pointer pointed to a Multidimensional array


Quote:

Originally Posted by ilikepython

I think you should be able to use Dpp as you use arr.

Nope. Dpp is a pointer to a pointer to a single int. arr is a one-dimensional array of 3 elements where each element is an array of 10 int.
Since the name of the array is the address of element 0, arr is the address of an array of 10 int. The address of an array of 10 int is not the same thing as a pointer to a pointer to a single int.

More info:
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
That is an array of 5 elements each of which is an int.

Expand|Select|Wrap|Line Numbers
  1. int array[];
  2.  
won't compile. You need to declare the number of elements.

Second, this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
is still an array of 5 elements. Each element is an array of 10 int.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10][15];
  2.  
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


Expand|Select|Wrap|Line Numbers
  1. int array[][10];
  2.  
won't compile. You need to declare the number of elements.

Third, the name of an array is the address of element 0
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
  3. int (*ptr)[10] = array;
  4.  
Fourth, when the number of elements is not known at compile time, you create the array dynamically:

Expand|Select|Wrap|Line Numbers
  1. int* array = new int[value][5];
  2. int (*ptr)[10] = new int[value][10];
  3. int (*ptr)[10][15] = new int[value][10][15];
  4.  
In each case value is the number of elements. Any other brackets only describe the elements.

Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

Expand|Select|Wrap|Line Numbers
  1. int** ptr = new int[value][10];    //ERROR
  2.  
new returns the address of an array of 10 int and that isn't the same as an int**.

Likewise:
Expand|Select|Wrap|Line Numbers
  1. int*** ptr = new int[value][10][15];    //ERROR
  2.  
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.
Reply