使用指针指向C中的一行
如果我有数组a
,如何将指针设置为第一行?
double a[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
If I have array a
, how would I set a pointer to the first row?
double a[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以声明指向行的指针并将其初始化以用以下行指向第一行:
由于阵列到驱动器decinter ,您还可以写:
括号是必要的,因为声明
声明了一系列指针,而声明则
声明了指向数组的指针。
You can declare a pointer to a row and initialize it to point to the first row with the following line:
Due to array to pointer decay, you can also write:
The parentheses are necessary, because the declaration
declares an array of pointers, whereas the declaration
declares a pointer to an array.
就像您对任何指针所做的那样,它正常地将其解雇。
*(a + 0)
给您矩阵的第一行,*(a + i)
将为您提供i-th
在2D数组中行。示例代码以获取2D数组每一行中的第一个元素看起来像这样。
输出:
Just dereference it normally as you would do to any pointer.
*(a + 0)
gives you the first row of the matrix, and*(a + i)
will give you thei-th
row in the 2D array.A sample code to get the first element in each row of your 2d array would look like this.
Output:
如果您有一个多维数组,例如
t
是某种类型的指示符,n1
,n2
,n3
,n4
是一些积极的整数,然后将指针指向数组的第一个元素,只需将左侧的尺寸更改为星号,例如在此声明中,数组设计师 a is隐式转换为指针转换为其第一个元素。
如果要获取一个指向i-th(
0< = i< n1
)元素的指针(这是类型t [n2]的数组] [n4]
)您可以写作或
这是一个演示程序。
程序输出是
If you have a multi-dimensional array like for example
where
T
is some type specifier andN1
,N2
,N3
,N4
are some positive integers then to make a pointer to the first element of the array just change the left most dimension to asterisk likeIn this declaration the array designator
a
is implicitly converted to a pointer to its first element.If you want to get a pointer to the i-th (
0 <= i < N1
) element of the array (that is an array of the typeT[N2][N3][N4]
) you can writeor
Here is a demonstration program.
The program output is
double a [2] [4]
=一个大小2的数组,每个类型double [4]
的项目。double(*p)[2] [4]
,并初始化/分配为p =&amp; a
。double A [4]
的指针被声明为double(*p)[4]
。的第一项double a [2] [4]
具有类型double [4]
,然后a
将腐烂到指向此类的指针一项,double(*)[4]
,无论何时在表达式中使用。因此,如果我们愿意,我们可以通过
double a [2] [4]
迭代:这也等同于
double(*p)[4] =&amp; a [0 ]
。现在,假设我们应该写一些带有saner语法的表达式:
任何
a [i]
表达式在定义上都是100%等于*(a+i)
。因此,以上等同于*(*(A+I)+J)
。这里使用两次指针算术:
a+i
是double( *)[4] [4]
类型,用i * sizeof(double)增加地址的指针算术[4])
字节。而+j
部分是double *
上的指针算术,而将地址用j * sizeof(double)
bytes增加。因此,
a [1] [0]
=(char *)A + 1 * sizeof(double [4]) + 0 * sizeof(double)
示例:
double a[2][4]
= an array with size 2, each items of typedouble[4]
.double (*p)[2][4]
and initialized/assigned asp=&a
.double a[4]
is declared asdouble (*p)[4]
.double a[2][4]
has typedouble [4]
, thena
will decay into a pointer to such an item,double (*)[4]
, whenever used in an expression.Therefore we can iterate through the
double a[2][4]
like this, if we wish:Which is also equivalent to
double (*p)[4]=&a[0]
.Now suppose that we write an expression some with saner syntax as we ought to:
Any
a[i]
expression is by definition 100% equivalent to*(a+i)
. So the above is equivalent to*(*(a+i)+j)
.Here pointer arithmetic is used twice:
a+i
is pointer arithmetic on adouble(*)[4]
type, increasing the address withi * sizeof(double[4])
bytes. Whereas the+j
part is pointer arithmetic on adouble*
, increasing the address withj * sizeof(double)
bytes.Thus
a[1][0]
=(char*)a + 1 * sizeof(double[4]) + 0 * sizeof(double)
Example: