英文:
Filling an array with pointers in C
问题
我得到了一个家庭作业任务,我需要用不重复的随机数字填充一个数组,但我不能使用[i]标识符,而是需要使用指针,我开始用相同的数字填充数组来看看指针的工作原理;我有这段代码(它只执行一次elementOfArray = 22
):
#include <stdio.h>
#define MAX_SIZE 10
void initializeArray(int *arr) {
int **elementOfArray = &arr;
for (int i = 0; i < MAX_SIZE; ++i) {
**elementOfArray = 22;
elementOfArray++;
}
}
int main() {
int arr[MAX_SIZE];
initializeArray(&arr[0]);
printf("%d\n", arr[0]);
printf("%d\n", arr[1]);
return 0;
}
如果我删除for循环内的elementOfArray++;
这一行,我可以改变arr[0]的值,但我不能移动到下一个位置,有人可以帮助我吗?谢谢
我在Stack Overflow上搜索到了这张表格:
ptr++; // 指针移动到下一个整数位置(就好像它是一个数组)
++ptr; // 指针移动到下一个整数位置(就好像它是一个数组)
++*ptr; // 增加ptr指向的值
++(*ptr); // 增加ptr指向的值
++*(ptr); // 增加ptr指向的值
*ptr++; // 指针移动到下一个整数位置(就好像它是一个数组)。但返回旧内容
(*ptr)++; // 增加ptr指向的值
*(ptr)++; // 指针移动到下一个整数位置(就好像它是一个数组)。但返回旧内容
*++ptr; // 指针移动到下一个整数位置,然后访问它,用你的代码,会导致段错误
*(++ptr); // 指针移动到下一个整数位置,然后访问它,用你的代码,会导致段错误
我尝试了ptr++、++ptr和*ptr++,但都没有起作用。
英文:
I got a homework assignment, I need to fill an array with no repeat random numbers but I can´t use the [i] intentifier, instead, i need to use pointers, i started filling the array with the same number to see how pointers works; i got this code (it just excecute once the elementOfArray = 22
):
#include <stdio.h>
#define MAX_SIZE 10
void initializeArray (int *arr) {
int **elementOfArray = &arr;
for (int i = 0; i < MAX_SIZE; ++i) {
**elementOfArray = 22;
elementOfArray++;
}
}
int main() {
int arr[MAX_SIZE];
initializeArray(&arr[0]);
printf("%d\n", arr[0]);
printf("%d\n", arr[1]);
return 0;
}
if i delete the line elementOfArray++;
inside the for, i can change the value of arr[0] but i can´t move to the next position, can somebody help me please? thanks
i've searched in stackoverflow, and found this table:
ptr++; // Pointer moves to the next int position (as if it was an array)
++ptr; // Pointer moves to the next int position (as if it was an array)
++*ptr; // The value pointed at by ptr is incremented
++(*ptr); // The value pointed at by ptr is incremented
++*(ptr); // The value pointed at by ptr is incremented
*ptr++; // Pointer moves to the next int position (as if it was an array). But returns the old content
(*ptr)++; // The value pointed at by ptr is incremented
*(ptr)++; // Pointer moves to the next int position (as if it was an array). But returns the old content
*++ptr; // Pointer moves to the next int position, and then get's accessed, with your code, segfault
*(++ptr); // Pointer moves to the next int position, and then get's accessed, with your code, segfault
i've tried ptr++, ++ptr, and *ptr++ and nothing works
答案1
得分: 0
elementOfArr
指向变量arr
。当你递增时,你只是创建了一个指向arr
位置的指针,而不是指向arr
所指向的数组的下一个元素的指针。你不能解引用这个指针,因为arr
只是一个单一的指针,而不是指针数组。
不需要使用elementOfArr
。只需递增arr
并解引用它。
void initializeArray(int *arr) {
for (int i = 0; i < MAX_SIZE; ++i) {
*arr = 22;
arr++;
}
}
在调用时,你可以直接使用arr
。当数组用作右值时,它会衰减为指向其第一个元素的指针。
initializeArray(arr);
英文:
elementOfArr
points to the variable arr
. When you increment, you're creating a pointer to just pass the location of arr
, not the next element of the array that arr
points to. You can't dereference this pointer, since arr
is just a single pointer, not an array of pointers.
There's no need for elementOfArr
. Just increment arr
and dereference that.
void initializeArray (int *arr) {
for (int i = 0; i < MAX_SIZE; ++i) {
*arr = 22;
arr++;
}
}
And in the call, you can just use arr
. When an array is used as an R-value, it decays to a pointer to its first element.
initializeArray(arr);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论