二维数组实现使用双指针 [英] Two Dimensional Array Implementation Using Double Pointer
问题描述
请考虑以下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;
}
有与我的code没有问题。它工作正常。
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)
=指向完全阻滞(第一阵列)结果 *(PTR + 1)
=指向完全区(第二阵列)。
*(ptr+0)
= Pointer to COMPLETE BLOCK (First Array)
*(ptr+1)
= Pointer to COMPLETE BLOCK (Second Array).
但是,什么是:<?code>(* PTR + 1)
GDB输出:
(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.
推荐答案
*(PTR + I)
是等于 PTR [I]
和 *(PTR + 1)
是 PTR [1]
。
您能想到的,一个2-D数组的数组的数组。
You can think, a 2-D array as array of array.
-
PTR
点完成2-D数组,所以PTR + 1
指向下一个2-D数组。
ptr
points to complete 2-D array, soptr+1
points to next 2-D array.
在下面 PTR
数字是2-D和列数是 3
In figure below ptr
is 2-D and number of columns are 3
由Kerrek SB先生,<一个做原创人物href=\"http://stackoverflow.com/questions/12674094/array-to-pointer-decay-and-passing-multidimensional-arrays-to-functions?answertab=oldest#tab-top\">here ,你也应该检查!
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]
了解以下内容:
PTR
分完成2-D。
* PTR = *(PTR + 0)= PTR [0]
这是第一行。
* PTR + 1 = PTR [1]
表示第二排线
*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
和GDB输出:
(gdb) p *(*ptr+1)
$1 = 2
这是正确的 2
这可以使用读 PTR [0] [1]
。
that is correct 2
this can be read using ptr[0][1]
.
这篇关于二维数组实现使用双指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!