Please consider the following code:
#include
#include
#define NUM_ARRAYS 4
#define NUM_ELEMENTS 4
#define INVALID_VAL
*(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].