【问题标题】:pointer to array of pointers argument vs local指向指针参数数组的指针与本地
【发布时间】:2019-04-03 03:50:51
【问题描述】:

我想将指针数组的地址分配给指向指针数组的指针。这就是我在下面为函数的参数所做的。它没有正常工作,它取消引用了不正确的值。我将问题缩小到下面的代码。但我不明白这个问题。

#include <stdio.h>

void test( char * arg[]){
    char * local[] = {"break", "the", "silence", NULL};
    char * (*argptr)[];
    char * (*localptr)[];
    argptr = &arg;
    localptr = &local;

    printf("address of arg %p\n", &arg);
    printf("address argptr is pointing to %p\n", argptr);

    printf("address of local %p\n", &local);
    printf("address localptr is pointing to %p\n", localptr);

    printf("arg %p\n", arg);
    printf("argptr is pointing to %p\n", *argptr);

      printf("local %p\n", local);
    printf("localptr is pointing to %p\n", *localptr);

}


int main()
{
    char * somecmd[] = {"words", "like", "violence", NULL};
    test(somecmd);

    return 0;
}

以上代码的输出:

address of arg 0x7ffd0ab3c418                                                                                                    
address argptr is pointing to 0x7ffd0ab3c418                                                                                     
address of local 0x7ffd0ab3c420                                                                                                  
address localptr is pointing to 0x7ffd0ab3c420                                                                                   
arg 0x7ffd0ab3c450                                                                                                               
argptr is pointing to 0x7ffd0ab3c418                                                                                             
local 0x7ffd0ab3c420                                                                                                             
localptr is pointing to 0x7ffd0ab3c420

arg 的地址和 argptr 的值是一样的。对于 local 的地址和 localptr 的值也是如此。但是,arg 的值和 argptr dereferenced 的值是不同的。这正是我所期待的。我期望的更像是 local 的值和 localptr dereferenced 的值,它们是相同的。

为什么 arg 和 argptr dereferenced 有不同的值?

【问题讨论】:

  • warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] argptr = &amp;arg;

标签: c arrays pointers arguments


【解决方案1】:

以下语句是不兼容的赋值。

argptr = &arg;

如果你打开了所有警告,你会注意到这样的事情:

 error: assignment from incompatible pointer type [-Wincompatible-pointer-types]
     argptr = &arg;

这是因为所谓的数组在作为参数传递时会衰减为指针,这发生在语句 test(somecmd);

所以在test 函数中你必须改变

char * (*argptr)[];

char ***argptr;

那么输出就会如预期的那样:

address of arg 000000000061FE10
address argptr is pointing to 000000000061FE10
address of local 000000000061FDD0
address localptr is pointing to 000000000061FDD0
arg 000000000061FE30
argptr is pointing to 000000000061FE30
local 000000000061FDD0
localptr is pointing to 000000000061FDD0

你会发现另一个类似这样的警告

 warning: format '%p' expects argument of type 'void *', but argument 2 has type 'char * (*)[4]' [-Wformat=]

这是因为转换说明符 %p 需要 void*。 所以在你所有的 printf 语句中,像这样对void* 进行强制转换:

printf("address of arg %p\n", (void*)&arg);

【讨论】:

  • 确实解决了它。我也确实有这些警告,但我不明白这些警告,因为类型看起来相同。我仍然很好奇允许指针保存数组地址并取消引用其他内容的机制是什么。这怎么可能?
猜你喜欢
  • 1970-01-01
  • 2016-01-27
  • 2017-09-03
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
相关资源
最近更新 更多