使用指针以下方式时究竟发生了什么?

huangapple go评论61阅读模式
英文:

what is happening exactly when i use pointers the following way?

问题

The code you provided is in C, and it involves pointers and array manipulation. The line you mentioned, *(pointerFunc + i), is used to access elements of the array pointed to by pointerFunc.

Here's a breakdown:

  • pointerFunc is a pointer to the beginning of the array returned by the func() function.
  • When you use pointerFunc + i, you are effectively moving the pointer i positions forward in memory. This allows you to access different elements of the array.
  • The *(pointerFunc + i) syntax is used to dereference the pointer at the current position (i.e., retrieve the value stored at that memory location).

So, in the context of your loop:

for (int i = 0; i < 10; i++ ) {
  printf("*(pointerFunc[%d]) : %d\n", i, *(pointerFunc + i));
}

*(pointerFunc + i) allows you to access each element of the array pointed to by pointerFunc one by one, and printf is used to print each element along with its index.

In summary, this code iterates through the array using pointer arithmetic, and *(pointerFunc + i) is how you access each element of the array through the pointer.

英文:
int * func()
{
    static int array[10];
    int i = 0;
    while(i&lt;10){
        array[i] = i + 1;
        i++;
    }
    return array;
}

int main()
{
    int *pointerFunc;
    pointerFunc = func();

    for (int i = 0; i &lt; 10; i++ ) {
      printf(&quot;*(pointerFunc[%d]) : %d\n&quot;, i, *(pointerFunc + i));
    }

    return 0;
}

I have a function which creates an array. In main, I want to print every element from that array. I used a pointer to get "access" to my array in the function. It does work the way it is, but I don't really understand why. To be precise, I wonder what is happening here (sure it prints the studd, but the pointer thing...):
printf(&quot;*(pointerFunc[%d]) : %d\n&quot;, i, *(pointerFunc + i));
I don't quite understand this:
*(pointerFunc + i)
It works, but how and why? It does print every element of my array, but I dont't get it.
What is the pointer "seeing"?

答案1

得分: 1

printf("*(pointerFunc[%d]) : %d\n", i, *(pointerFunc + i));

这里,你正在使用指针算术来访问数组的每个元素。

表达式 *(pointerFunc + i)i 添加到存储在 pointerFunc 中的内存地址,然后取消引用结果的内存地址以访问存储在该内存位置的值。由于 pointerFunc 指向数组的第一个元素,*(pointerFunc + i) 将为您提供数组的第 i 个元素中存储的值。

或者,您还可以使用数组下标运算符 [] 来使用指针变量访问数组的元素,如下所示:

printf("pointerFunc[%d] : %d\n", i, pointerFunc[i]);

这两个表达式是等效的,并将为您提供数组的第 i 个元素中存储的值。

英文:
printf(&quot;*(pointerFunc[%d]) : %d\n&quot;, i, *(pointerFunc + i));

Here, you are using pointer arithmetic to access each element of the array.

The expression *(pointerFunc + i) adds i to the memory address stored in pointerFunc, and then dereferences the resulting memory address to access the value stored in that memory location. Since pointerFunc points to the first element of the array, *(pointerFunc + i) gives you the value stored in the i-th element of the array.

Alternatively, you could also use the array subscript operator [] to access the elements of the array using the pointer variable, like this:

printf(&quot;pointerFunc[%d] : %d\n&quot;, i, pointerFunc[i]);

Both expressions are equivalent and will give you the value stored in the i-th element of the array.

答案2

得分: 0

pointerFunc(名称有点不准确)被设置为func()返回的值,即func()中的&amp;array[0]。由于array是静态的,它是持久的;在func()返回后,为其分配的内存仍然可以访问(相对于自动数组,后者会导致未定义行为)。然后,您的主循环使用pointerFunc打印array[0]array[9]*(pointerFunc + i)等同于pointerFunc[i]。前者实际上是pointerFunc[i]如何被评估的,所以i[pointerFunc]也是等价的。虽然不推荐使用,但有助于理解。

英文:

pointerFunc (a bit of a misnomer) is set to the returned value of func(), which is &amp;array[0] in func(). Since array is static, it is persistent; the memory allocated for it is still accessible after func() returns (vs. an automatic array, which would be UB). Your main loop then prints array[0] through array[9] using pointerFunc. *(pointerFunc + i) is equivalent to pointerFunc[i]. The former is how pointerFunc[i] is actually evaluated, so i[pointerFunc] is also equivalent. Not recommended but good for understanding.

huangapple
  • 本文由 发表于 2023年5月6日 19:21:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定