简体   繁体   English

指向数组元素地址的指针

[英]Pointer to the Address of Element of an Array

int s[4][2]= {
             {1234,56},
             {1212,33},
             {1434,80},
             {1312,78}
             };
int i,j;
for(i=0;i<=3;i++)
{
    printf("\n");
    for(j=0;j<=1;j++)
    {
        printf("%d  ",*(s[i]+j));
    }
}

Output Showing is 现显示为

 1234,56 1212,33 1434,80 1312,78 

As we know *(&Variable) will print the Value of the Variable But when We implement the same concept in above program... 我们知道*(&Variable)将打印*(&Variable)的值,但是当我们在上述程序中实现相同的概念时...

int s[4][2]= {
             {1234,56},
             {1212,33},
             {1434,80},
             {1312,78}
             };
int i,j;
for(i=0;i<=3;i++)
{
    printf("\n");
    for(j=0;j<=1;j++)
    {
        printf("%d  ",*(&s[i]+j));
    }
}

output is showing the Address of each element of array. 输出显示数组中每个元素的地址。

Why this is happening? 为什么会这样呢? Why Output is not equal to value of elements of Array?? 为什么输出不等于数组元素的值?

Notice here , s is a two dimensional array, and array names decay to pointer to the first element, in certain cases. 请注意,这里 s是一个二维数组,在某些情况下,数组名称会衰减为指向第一个元素的指针。

Here, s[i] is of type array, array of two int s. 此处, s[i]是数组类型,两个int的数组。

Then, quoting C11 , chapter §6.3.2.1, 然后,在第6.3.2.1节中引用C11

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue. 除非它是sizeof运算符, _Alignof运算符或一元&运算符的操作数,或者是用于初始化数组的字符串文字,否则将类型为“ array of type”的表达式转换为带有键入“指向类型的指针” ,它指向数组对象的初始元素,而不是左值。 [...] [...]

Thus, 从而,

  • In the first case, 在第一种情况下,

     *(s[i]+j) 

    s[i] is of type int [2] , which decays to an int * , which is used in the pointer arithmetic, producing another int * . s[i]的类型为int [2] ,它衰减为一个int * ,该int *用于指针算术,从而生成另一个int * After the dereference, you get an int . 取消引用后,您得到一个int

  • In the second case, 在第二种情况下

     *(&s[i]+j) 

    due to the same aforesaid reason, in case of &s[i] , s[i] does not decay , and the type is "pointer to int [2] " and the pointer arithmetic works on that. 由于相同的原因,在&s[i]情况下s[i]不会衰减 ,并且类型为“指向int [2]的指针”,并且指针算法可以对此进行处理。 So, in this case, the result of the addition is also "pointer to int [2] " and after the final dereference, it becomes int * , which is the mismatch for %d and that is what reported by your compiler. 因此,在这种情况下,加法的结果也是“指向int [2]指针”,并且在最终取消引用之后,它变为int * ,这与%d不匹配,这就是编译器报告的结果。

Your array is bi-dimensional. 您的数组是二维的。 So, s[i] is the address of line i (ie s[i] is of type int * ), and &s[i] is an address of an address (ie of type int ** ). 所以, s[i]是线的地址i (即s[i]的类型为int * ),和&s[i]是一个地址的一个地址(即类型的int ** )。 When you apply * on it, you get an address (ie int * ). 在上面加上*时,您会得到一个地址(即int * )。

It is true that the operator & means "address-of" and *(&x) refers to the value of x . 确实,运算符&意思是“ address-of”,而*(&x)表示x的值。

First case 第一种情况

Your print statement is this. 您的打印声明是这样的。

printf("%d  ",*(s[i]+j));

Here s[i]+j is the address. s [i] + j是地址。

lets consider i=0,j=1 , first i loop, second j loop. 让我们考虑i = 0,j = 1,第一个i循环,第二个j循环。

here s[i] will be the address in which value 1234 is residing. s [i]将是值1234所在的地址。 lets say it is 489000. so s[i]+j , here s[0]+1 will be the address at which value 56 is residing. 假设它是489000。因此s [i] + j,这里s [0] +1将是值56所在的地址。 lets say it is 489004 可以说是489004

So, *(s[i]+j) ,ie; 因此,*(s [i] + j),即; here *(s[0]+1) will be giving the value at address 489004. ie 56. So here it will print the values 1234, 56, 1212 etc. 此处*(s [0] +1)将给出地址489004的值。即56。因此,此处将打印值1234、56、1212等。

Second Case Your print statement is this. 第二种情况您的打印声明是这样。

printf("%d  ",*(&s[i]+j));

Here &s[i]+j is the address. 这里&s [i] + j是地址。

lets consider i=0,j=0 , first i loop, first j loop. 让我们考虑i = 0,j = 0,第一个i循环,第一个j循环。

here s[i] will be the address in which value 1234 is residing. s [i]将是值1234所在的地址。 Lets say it is 489000. So &s[i] will be the address in which address 489000 is residing. 假设它是489000。因此&s [i]将是地址489000所在的地址。 Lets say it is 380000. Recall the concepts of pointer's. 可以说是380000。回想一下指针的概念。

So &s[i]+j will be "address in which address 489000 is residing + j" . 因此&s [i] + j将是“地址489000所在的地址+ j”。 ie; 380000+j 380000 + j

we are applying a value at operator * on this particular 'address' we got. 我们在运算符*上对这个特定的“地址”应用了一个值。 *(&s[i]+j). *(&s [i] + j)。 So it becomes, Value at['address in which address 489000 is residing' + j]. 因此,其值为['地址489000所在的地址'+ j]。 Value at[380000 + j]. 值为[380000 + j]。

when i=0,j=0, it will print, Value At[380000] , ie; 当i = 0,j = 0时,将打印Value At [380000],即; 489000. 489000。

So it will print the address in which the values 1234, 56 , 1212 etc are stored. 因此它将打印存储值1234、56、1212等的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM