用C语言填充一个指针数组

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

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 &lt;stdio.h&gt;

#define MAX_SIZE 10

void initializeArray (int *arr) {
    int **elementOfArray = &amp;arr;
    for (int i = 0; i &lt; MAX_SIZE; ++i) {
        **elementOfArray = 22;
        elementOfArray++;
    }
}

int main() {
    int arr[MAX_SIZE];
    initializeArray(&amp;arr[0]);

    printf(&quot;%d\n&quot;, arr[0]);
    printf(&quot;%d\n&quot;, 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&#39;s accessed, with your code, segfault
*(++ptr); // Pointer moves to the next int position, and then get&#39;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 &lt; 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);

huangapple
  • 本文由 发表于 2023年6月9日 07:54:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436382.html
匿名

发表评论

匿名网友

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

确定