Two Dimensional Array Implementation Using Double Pointer

不问归期 提交于 2019-11-28 04:01:38
Grijesh Chauhan

*(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].

MOHAMED
                                 (*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

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");
}
}
MJZ

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!