C语言 使用双指针实现二维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13974001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Two Dimensional Array Implementation Using Double Pointer
提问by Sandeep Singh
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).
*(ptr+0)= 指向完整块(第一个数组)的*(ptr+1)指针 = 指向完整块(第二个数组)的指针。
But what is: (*ptr+1)?
但什么是:(*ptr+1)?
GDB Output:
GDB 输出:
(gdb) p *(*ptr+1)
= 2
(gdb) p *(*ptr+2)
= 3
(gdb) p *(*ptr+3)
= 4
(gdb) p *(*ptr+4)
= 0
I am getting confused on this. Please provide me some explanation to resolve this doubt.
我对此感到困惑。请给我一些解释以解决这个疑问。
回答by 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
所以如果你已经定义了一个双指针作为这个二维数组的指针,让我们说 int **ptr
so ptris ponting to the main array which will contains pointers to sub arrays. ptris ponting to the main array that's means ptris pointing to the first element of the main array so ptr + 1is pointing to the second element of the main array.
所以ptr指向将包含指向子数组的指针的主数组。指向ptr主数组的意思ptr是指向主数组的第一个元素,因此ptr + 1指向主数组的第二个元素。
*ptrthis means the content of the first element which the ptris pointing on. And it is a pointer to a subarray. so *ptris a pointer to the first subarray (the subarray is an array of int). so *ptris pointing to the first element in the first subarray. so *ptr + 1is a pointer to the second element in the first subarray
*ptr这意味着ptr指向的第一个元素的内容。它是一个指向子数组的指针。so*ptr是指向第一个子数组的指针(子数组是 的数组int)。so*ptr指向第一个子数组中的第一个元素。so*ptr + 1是指向第一个子数组中第二个元素的指针
回答by Grijesh Chauhan
*(ptr+i)is equals to ptr[i]and
*(ptr+1)is ptr[1].
*(ptr+i)等于ptr[i]并且
*(ptr+1)是ptr[1]。
You can think, a 2-D array as array of array.
你可以认为,一个二维数组就是数组的数组。
ptrpoints to complete 2-D array, soptr+1points to next 2-D array.
ptr指向完整的二维数组,所以ptr+1指向下一个二维数组。
In figure below ptris 2-D and number of columns are 3
下图中 ptr是二维的,列数是3
Original figure made by Mr. Kerrek SB, here, you should also check!
Kerrek SB 先生的原创图,在这里,你也应该检查一下!
+===============================+==============================+====
|+---------+----------+--------+|+----------+---------+--------+|
||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+1) = *( ptr[0] + 1 ) = ptr[0][1]
Understand following:
了解以下内容:
ptrpoints to complete 2-D.
ptr点完成二维。
*ptr = *(ptr + 0) = ptr[0]that is first row.
*ptr = *(ptr + 0) = ptr[0]那是第一行。
*ptr + 1 = ptr[1]means second row
*ptr + 1 = ptr[1]表示第二行
*(*ptr+1) = *(*(ptr + 0) + 1 ) = *(ptr[0] + 1) = ptr[0][1]
*(*ptr+1) = *(*(ptr + 0) + 1 ) = *(ptr[0] + 1) = ptr[0][1]
Array 0 Elements:
1 2 3 4
And GDB Output:
和 GDB 输出:
(gdb) p *(*ptr+1)
= 2
that is correct 2this can be read using ptr[0][1].
这是正确的,2可以使用ptr[0][1].
回答by Tonmoy
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");
}
}
回答by MJZ
Unless you mistypes, (*ptr + 1)is equivalent to *(ptr + 0) + 1which is a pointer to the second element in the first block.
除非你打错了,(*ptr + 1)等价于*(ptr + 0) + 1which 是指向第一个块中第二个元素的指针。

