递增指针数组

时间:2019-07-01 22:20:31

标签: c

我一直在尝试增加指向的指针数组并将其分配为数组的地址,但是我似乎无法弄清楚。

输入:

void * myParam(unsigned int *argv[]){

    for(unsigned int i = 0; i < 5; i++){
        printf("Block[%d] = Pointer Address %x\n", i, *(argv + i));
    }

    return *argv;
}



int main(){

    unsigned int arr[5] = {1,2,3,4,5};
    unsigned int *ptr;
    ptr = arr;
    ptr = myParam(&ptr);

}

输出:

Block[0] = Pointer Address ea6d6b60

Block[1] = Pointer Address 1

Block[2] = Pointer Address 3

Block[3] = Pointer Address 5

Block[4] = Pointer Address f6770089

1 个答案:

答案 0 :(得分:1)

您没有指针数组。您有一个指向一个指针的指针(=一个指针的数组)。 下标argv的下标为0(如果未取消引用则为1)是不确定的。取消引用一次后,可以添加0到4(也可以取消引用那些值以获得数组值(1到5))。

#include <stdio.h>
void * myParam(unsigned int *argv[]){

    for(unsigned int i = 0; i < 5; i++){
        printf("Block[%u] = Pointer Address %p, value=%u\n", i, (void*)(*argv + i), 
                                                                (*argv)[i]);
    }

    return *argv;
}

int main(){

    unsigned int arr[5] = {1,2,3,4,5};
    unsigned int *ptr;
    ptr = arr;
    ptr = myParam(&ptr);
}