Implementing a two-dimensional array using a double pointer

Pay attention to 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 are no problems in my code. It works great.

 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 got confused about this. Please provide me some explanation to resolve this doubt.

+6
source share
4 answers

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

You may think that a 2-dimensional array is an array of an array.

  • ptr points to a full 2-dimensional array, so ptr+1 points to the next 2-dimensional array.

In the picture below ptr displays 2-D and the number of columns is 3

The original figure made by Mr. Kerrek S.B., 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 the following:

ptr indicates the completion of 2-D.

*ptr = *(ptr + 0) = ptr[0] This is the first line.

*ptr + 1 = ptr[1] means the second line

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

 Array 0 Elements: 1 2 3 4 

And the output of GDB:

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

which is true 2 , this can be read using ptr[0][1] .

+9
source
  (*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 

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

so if you define a double pointer as a pointer to this two-dimensional array, say int **ptr

thus, ptr is responsible for the main array, which will contain pointers to subarrays. ptr points to the main array, which means that ptr points to the first element of the main array, so ptr + 1 points to the second element of the main array.

*ptr this means the contents of the first element pointed to by ptr . And this is a pointer to a subarray. therefore *ptr is a pointer to the first subarray (the subarray is an array of type int ). therefore *ptr points to the first element in the first subarray. therefore *ptr + 1 is a pointer to the second element in the first subarray

+25
source

The easiest way to create a 2-dimensional array using a pointer, assign values, and access 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"); } } 
+3
source

If you are not mistaken, (*ptr + 1) equivalent to *(ptr + 0) + 1 , which is a pointer to the second element in the first block.

+2
source

All Articles