指针下标从负值开始 [英] pointer subscript starts from negative value
问题描述
int nrow=5,ncol=7,i,j;
float **ptr;
/*allocation*/
ptr=(float **) malloc(nrow*sizeof(float*));
ptr[0]=(float *) malloc(nrow*ncol*sizeof(float));
/*initialize*/
for (i=0;i<nrow;i++)
for (j=0;j<ncol;j++) ptr[i][j]=0.0;
在上述情况下,我们知道行从0
开始并以nrow-1
结尾,列从0
开始并以ncol-1
结尾.但是如何让行从-4
开始并在nrow+3
结束,又如何使列从-4
开始并在ncol+3
结束?
We know in the above case, the row starts from 0
and ends in nrow-1
, the column starts from 0
and ends in ncol-1
. But how can I let the row start from -4
and end in nrow+3
, also let the column start from -4
and end in ncol+3
?
补充代码:
#include <stdio.h>
#include <stdlib.h>
float *vector(int nl, int nh){
/* allocate a float vector with subscript range v[nl..nh] and initializing
this vector, eg. vector[nl..nh]=0.0 */
float *v;
int i,NR_END=0;
v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
for (i=0;i<(nh-nl+1+NR_END);i++) v[i]=0.0;
return v-nl+NR_END;
}
int main(int argc, char *argv[])
{
int i,nrow=5, row1, row2;
float *v;
row1=-4;
row2=nrow+3;
v = vector(row1,row2);
for (i=-4;i<(nrow+4);i++) {
v[i]=(float)i;
printf("v[%d]=%f\n",i,v[i]);
}
exit(0);
}
如果我运行上面的代码,它将得到正确的答案:
If I run the above code, it'll get the correct answer:
v[-4]=-4.000000
v[-3]=-3.000000
v[-2]=-2.000000
v[-1]=-1.000000
v[0]=0.000000
v[1]=1.000000
v[2]=2.000000
v[3]=3.000000
v[4]=4.000000
v[5]=5.000000
v[6]=6.000000
v[7]=7.000000
v[8]=8.000000
推荐答案
比方说,您的数组有7个元素,其布局如下:
Let's say your array has 7 elements and is laid out as below:
+-----+-----+-----+-----+-----+-----+-----+
| | | | | | | |
+-----+-----+-----+-----+-----+-----+-----+
如果指针指向数组的第一个元素,则可以将其索引为:
If a pointer points to the first element of the array, it can be indexed as:
p
|
v
+-----+-----+-----+-----+-----+-----+-----+
| | | | | | | |
+-----+-----+-----+-----+-----+-----+-----+
p[0] p[1] p[2] p[3] p[4] p[5] p[6]
如果指针指向数组中间的元素,则可以使用负值对其进行索引.
If a pointer points to an element in the middle of the array, it can be indexed using negative values.
p
|
v
+-----+-----+-----+-----+-----+-----+-----+
| | | | | | | |
+-----+-----+-----+-----+-----+-----+-----+
p[-3] p[-2] p[-1] p[0] p[1] p[2] p[3]
如果指针指向最后一个元素之后的一个元素,则只能使用负值对其进行索引.
If the pointer points to one element past the last element, it can be indexed using only negative values.
p
|
v
+-----+-----+-----+-----+-----+-----+-----+
| | | | | | | |
+-----+-----+-----+-----+-----+-----+-----+
p[-7] p[-6] p[-5] p[-4] p[-3] p[-2] p[-1]
先行者指向第一个元素之前的任何内容都是无效的.因此,无论指针指向元素的有效范围内的什么位置,有效索引都不能小于-7或大于6.
It is not valid for the pionter to point to anything before the first element. Hence, no matter where the pointer points to in the valid range of elements, a valid index cannot be less than -7 or greater than 6.
提出您的问题
但是如何让行从
-4
开始并以nrow+3
结束,又如何使列从-4
开始并以ncol+3
结束?
But how can I let the row start from
-4
and end innrow+3
, also let the column start from-4
and end inncol+3
?
您不能.如果指针指向数组的第5个元素,则可以使用-4作为有效索引,但是结束条件将为nrow-4
/ncol-4
. nrow + <some number>
/ncol + <some number>
永远不会是正确的结束索引.
You cannot. If a pointer points to the 5-th element of the array, you can use -4 as a valid index but then the end condition is going to be nrow-4
/ncol-4
. nrow + <some number>
/ncol + <some number>
will never be the correct end index.
float** ptr1 = &(5-th row of the array);
for ( int i = -4; i < nrow - 4; ++i )
{
// OK to use ptr1[i];
float* ptr2 = &(5-the element/column of the row)
for ( int j = -4; j < ncol - 4; ++j )
{
// OK to use ptr2[j];
}
}
这篇关于指针下标从负值开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!