Two Dimensional Array Implementation Using Double Pointer
Asked Answered
P

5

9

Please consider the following code:

#include <stdio.h>
#include <stdlib.h>

#define NUM_ARRAYS     4
#define NUM_ELEMENTS   4
#define INVALID_VAL   -1

int main()
{
   int index            = INVALID_VAL;
   int array_index      = INVALID_VAL;
   int **ptr            = NULL;

   ptr = malloc(sizeof(int*)*NUM_ARRAYS);

   if (!ptr)
   {
      printf ("\nMemory Allocation Failure !\n\n");
      exit (EXIT_FAILURE);
   }

   for (index=0; index<NUM_ARRAYS; index++)
   {
      *(ptr+index) = malloc(sizeof(int)*NUM_ELEMENTS); 

      if (!*(ptr+index))
      {
         printf ("\nMemory Allocation Failure !\n");
         exit (EXIT_FAILURE);
      }
   }

   /* Fill Elements Into This 2-D Array */
   for (index=0; index<NUM_ARRAYS; index++)
   {
      for (array_index = 0; array_index<NUM_ELEMENTS; array_index++)
      {
         *(*(ptr+index)+array_index) = (array_index+1)*(index+1);
      }
   }

   /* Print Array Elements */
   for (index = 0; index<NUM_ARRAYS; index++)
   {
      printf ("\nArray %d Elements:\n", index);
      for (array_index = 0; array_index<NUM_ELEMENTS; array_index++)
      {
         printf (" %d ", *(*(ptr+index)+array_index));
      }
      printf ("\n\n");
   }

   return 0;
}

There is no problem with my code. It works fine.

Output:

Array 0 Elements:
 1  2  3  4 


Array 1 Elements:
 2  4  6  8 


Array 2 Elements:
 3  6  9  12 


Array 3 Elements:
 4  8  12  16 

I have a question about pointer arithmetic:

*(ptr+0) = Pointer to COMPLETE BLOCK (First Array)
*(ptr+1) = Pointer to COMPLETE BLOCK (Second Array).

But what is: (*ptr+1)?

GDB Output:

(gdb) p *(*ptr+1)
$1 = 2
(gdb) p *(*ptr+2)
$2 = 3
(gdb) p *(*ptr+3)
$3 = 4
(gdb) p *(*ptr+4)
$4 = 0

I am getting confused on this. Please provide me some explanation to resolve this doubt.

Passionate answered 20/12, 2012 at 14:11 Comment(2)
I don't see (*ptr+1) in this code.Kieserite
No, Its NOT there. This is my Conceptual Doubt after writing this CodePassionate
C
9

*(ptr+i) is equals to ptr[i] and *(ptr+1) is ptr[1].

You can think, a 2-D array as array of array.

  • ptr points to complete 2-D array, so ptr+1 points to next 2-D array.

In figure below ptr is 2-D and number of columns are 3

Original figure made by Mr. Kerrek SB, here , you should also check!

+===============================+==============================+====
|+---------+----------+--------+|+----------+---------+--------+|
||ptr[0,0] | ptr[0,1] | ptr[0,2]|||ptr[1,0] |ptr[1,1] | ptr[1,2]|| ...
|+---------+----------+--------+++----------+---------+--------++ ...
|            ptr[0]             |           ptr[1]              |
+===============================+===============================+====
   ptr

*(*ptr+1) = *( ptr[0] + 1 ) = ptr[0][1]

Understand following:

ptr points to complete 2-D.

*ptr = *(ptr + 0) = ptr[0] that is first row.

*ptr + 1 = ptr[1] means second row

*(*ptr+1) = *(*(ptr + 0) + 1 ) = *(ptr[0] + 1) = ptr[0][1]

Array 0 Elements:
1  2  3  4 

And GDB Output:

(gdb) p *(*ptr+1)
$1 = 2  

that is correct 2 this can be read using ptr[0][1].

Croaker answered 20/12, 2012 at 14:18 Comment(6)
I have no problem in understanding of *(ptr+1). In fact, I have written the above code myself. My problem is {*(ptr)+1}Passionate
Thanks Grijesh. But gdb shows a different output. I am updating my Question to include GDB O/P for *(*ptr+1).Passionate
No. it is still not clear to me. I also expected that (*ptr)+1 may go to SECOND Array Block, but it is going to SECOND Element of FIRST Array (Added GDB Output in the Question above.)Passionate
@SandeepSingh : please check know it I hope now understood to you.Croaker
@SandeepSingh what is status nowCroaker
@SandeepSingh: welcom sandeep ..Initially it was my mistake I written wrong sorry for that. ...& Thanks to you. let me know if you have other doubt.Croaker
N
28
                                 (*ptr)      (*ptr+1)     (*ptr+2)
                                   |            |            |
             __________      ______v____________v____________v____________
  ptr------>|   *ptr   |--->|  *(*ptr)   |  *(*ptr+1)  |*(*ptr+2) |       |
            |__________|    |____________|_____________|__________|_______|
 (ptr+1)--->| *(ptr+1) |     ____________ _____________ __________________
            |__________|--->|*(*(ptr+1)) |*(*(ptr+1)+1)|          |       |
            |          |    |____________|_____________|__________|_______|
            |__________|          ^             ^
                                  |             |
                              *(ptr+1)     *(ptr+1)+1

2D array with double pointers that means that you have a main array and the elements of the main array are pointers (or addresses) to a sub arrays. As indicated in above figure

so if you have defined a double pointer as a pointer of this 2D array let's say int **ptr

so ptr is ponting to the main array which will contains pointers to sub arrays. ptr is ponting to the main array that's means ptr is pointing to the first element of the main array so ptr + 1 is pointing to the second element of the main array.

*ptr this means the content of the first element which the ptr is pointing on. And it is a pointer to a subarray. so *ptr is a pointer to the first subarray (the subarray is an array of int). so *ptr is pointing to the first element in the first subarray. so *ptr + 1 is a pointer to the second element in the first subarray

Neighbors answered 20/12, 2012 at 14:45 Comment(1)
+1 to your answer, your firgure is much clear then my. Kallel how to draw this fig. ? do you use any tool?Croaker
C
9

*(ptr+i) is equals to ptr[i] and *(ptr+1) is ptr[1].

You can think, a 2-D array as array of array.

  • ptr points to complete 2-D array, so ptr+1 points to next 2-D array.

In figure below ptr is 2-D and number of columns are 3

Original figure made by Mr. Kerrek SB, here , you should also check!

+===============================+==============================+====
|+---------+----------+--------+|+----------+---------+--------+|
||ptr[0,0] | ptr[0,1] | ptr[0,2]|||ptr[1,0] |ptr[1,1] | ptr[1,2]|| ...
|+---------+----------+--------+++----------+---------+--------++ ...
|            ptr[0]             |           ptr[1]              |
+===============================+===============================+====
   ptr

*(*ptr+1) = *( ptr[0] + 1 ) = ptr[0][1]

Understand following:

ptr points to complete 2-D.

*ptr = *(ptr + 0) = ptr[0] that is first row.

*ptr + 1 = ptr[1] means second row

*(*ptr+1) = *(*(ptr + 0) + 1 ) = *(ptr[0] + 1) = ptr[0][1]

Array 0 Elements:
1  2  3  4 

And GDB Output:

(gdb) p *(*ptr+1)
$1 = 2  

that is correct 2 this can be read using ptr[0][1].

Croaker answered 20/12, 2012 at 14:18 Comment(6)
I have no problem in understanding of *(ptr+1). In fact, I have written the above code myself. My problem is {*(ptr)+1}Passionate
Thanks Grijesh. But gdb shows a different output. I am updating my Question to include GDB O/P for *(*ptr+1).Passionate
No. it is still not clear to me. I also expected that (*ptr)+1 may go to SECOND Array Block, but it is going to SECOND Element of FIRST Array (Added GDB Output in the Question above.)Passionate
@SandeepSingh : please check know it I hope now understood to you.Croaker
@SandeepSingh what is status nowCroaker
@SandeepSingh: welcom sandeep ..Initially it was my mistake I written wrong sorry for that. ...& Thanks to you. let me know if you have other doubt.Croaker
M
3

Simplest way for creating 2-dimensinal array using pointer,assigning values and accessing elements from the array.

#include<stdio.h>
#include<stdlib.h>

int main()
{
int i,j;
int row,col;
printf("Enter the values for row and col:\n");
scanf("%d%d",&row,&col);
int **arr=(int**)malloc(row*(sizeof(int*)));
for(i=0;i<row;i++)
{
    *(arr+i)=(int*)malloc(sizeof(int)*col);
            //You can use this also. Meaning of both is same.
            //arr[i]=(int*)malloc(sizeof(int)*col);
}
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
    arr[i][j]=0;
}
for(i=0;i<row;i++)
{
    for(j=0;j<col;j++)
    {
        printf("%d ",arr[i][j]);
    }
    printf("\n");
}
}
Military answered 17/10, 2013 at 16:22 Comment(1)
Yes. You have simplified the assignment of values to double pointer. But there is a great danger to this simplicity in understanding a double pointer. From looking at this code, many people can conclude that a double pointer to a variable is like 2 dimensional array. But double pointer to a variable is not same as 2-D array.Malvasia
V
2

Unless you mistypes, (*ptr + 1) is equivalent to *(ptr + 0) + 1 which is a pointer to the second element in the first block.

Vow answered 20/12, 2012 at 14:15 Comment(1)
No, I have NOT mistyped. I had confusion over {(*ptr)+1}. Your interpretation is correct (verified by GDB). But as per my understanding, *ptr = Pointer to First ARRAY BLOCK, NOT to any individual element. Then, (*ptr+1) may have gone to the beginning of second array. Please clear this doubt.Passionate
C
1

*ptr + 1 is address of ptr[0][1]

Because, we know

ptr[0][1] == *(*(ptr+0)+1)

Now putting ampersand on both side

&ptr[0][1] == &*(*(ptr+0)+1)

You know that ampersand with asterisk is like plus sign with minus sign? They get cancelled. So this becomes

&ptr[0][1] == (*(ptr+0)+1)

Which is same as

&ptr[0][1] == *(ptr+0)+1

Which is again same as

&ptr[0][1] == *ptr+1

This is my first statement.

Callable answered 5/8, 2020 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.