英文:
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 thefunc()
function.- When you use
pointerFunc + i
, you are effectively moving the pointeri
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<10){
array[i] = i + 1;
i++;
}
return array;
}
int main()
{
int *pointerFunc;
pointerFunc = func();
for (int i = 0; i < 10; i++ ) {
printf("*(pointerFunc[%d]) : %d\n", 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("*(pointerFunc[%d]) : %d\n", 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("*(pointerFunc[%d]) : %d\n", 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("pointerFunc[%d] : %d\n", 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()
中的&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 &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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论