【问题标题】:Can you return a type of pointer to pointer in a function?你能在函数中返回一种指向指针的指针吗?
【发布时间】:2015-09-12 22:15:07
【问题描述】:

我知道你可以从函数中返回一种指针。 前任。 void *foo() 你能在函数中返回一种指向指针的指针吗? 前任。 void **foo2()

这里是关于我的问题的更多信息:

我尝试将 ptr-to-ptr tmp2 分配给 blocks[i][j],然后返回 tmp2。 blocks[i][j] 也是一个ptr-to-ptr。

我对将 ptr 操作为 ptr-to-ptr 感到困惑:我不确定 return ((tmp2+i)+j); 是否是导致 printf("2---%d\n", **tmpPtr2); 行分段错误的原因。为了调试,我尝试打印:printf("%d\n", *( (*(tmp2+i)) +j) ); 但是,它会导致新的分段错误。

#include <stdio.h>
#include <stdlib.h>

int **blocks, **tmp2;
int n = 10;

int **findBlock2(int b){
    int i, j ;

    for (i=0; i<n; i++){
        for (j=0; j<n; j++){
            if (blocks[i][j]==b){
                printf("%d\n", blocks[i][j]);

                //Segmentation fault
                printf("%d\n", *((*(tmp2+i))+j) );

                return ((tmp2+i)+j);
            }
        }
    }
    return NULL;
}

int main(int argc, const char * argv[]) {
    int i, j;
    int **tmpPtr2;

    //allocate memory space and assign a block to each index
    blocks=malloc(n * sizeof *blocks);
    for (i=0; i<n; i++) {
        blocks[i]=malloc(n * sizeof(*blocks[i]));
        blocks[i][0]=i;
    }

    if ((tmpPtr2=findBlock2(4))==NULL)    return -1;

    //Segmentation Fault
    printf("2---%d\n", **tmpPtr2);

    return 0;
}

更新回答我的问题:

(1) 将 ttmp2=blocks; 添加到 findBlock2() 的顶部会删除两个段错误。

(2) return ((tmp2+i)+j); 展示了如何操作指向 ptr-to-ptr 或 2D 数组的 ptr-to-ptr

(3) printf("%d\n", *( (*(tmp2+i)) +j) ); 展示了如何执行 (2) 并取消引用它。

希望对他人有所帮助

【问题讨论】:

  • 你的意思是指向函数指针的指针吗?还是给出的答案是您想要的?
  • 答案是肯定的,但是您可能有一个相关的编码问题正在尝试解决,所以发布您的代码和具体问题,您会得到比刚才更好的答案是 + 几个很可能与您的问题无关的示例。
  • 你们两个是对的,让我更新我的问题。 :)
  • 使用[ ] 表示法而不是*((*((s)+(y))+1) 或其他,更容易阅读
  • 你得到一个段错误,因为你从来没有在任何地方指向 tmp2,但是你在 findBlock2 中取消引用它

标签: c pointers


【解决方案1】:

是的,就像使用任何指针变量一样。

#include <stdio.h>
#include <stdlib.h>

int ** function(){
    int ** matrix = malloc(sizeof(int*));
    *matrix = malloc(sizeof(int));
    matrix[0][0] = 5;
    return matrix;
}

int main()
{
    int **matrix = function();
    printf("%d",matrix[0][0]);
    free(matrix[0]);
    free(matrix);
    return 0;
}

添加到另一部分。在您的函数findBlock2 中,除了访问已经指出的无效引用之外,您的目标似乎是返回对满足if 语句的块的引用。如果是这种情况,那么返回一个指向 int* 的指针就足够了。

int *findBlock2( int b )

/////////////////

return ( *(blocks+i)+j );

【讨论】:

    【解决方案2】:

    您可能想要一个 2D 数组,而不是一些缓慢、碎片化的查找表。在这种情况下,请这样做:

    #include <stdlib.h>
    
    void* alloc_2D (size_t x, size_t y)
    {
      return malloc (sizeof (int[x][y]));
    }
    
    int main (void)
    {
      const size_t X = 5;
      const size_t Y = 3;
    
      int (*arr_2D)[Y] = alloc_2D(X, Y); 
    
     // X dimension was omitted in declaration to make array syntax more intuititve:
    
      arr_2D[i][j] = something;
    
      ...
      free(arr_2D);
    }
    

    【讨论】:

      【解决方案3】:

      答案是“是”。请参考以下代码:

      #include <stdio.h>
      #include <malloc.h>
      
      void ** foo2(void){
          int **p = malloc(sizeof(*p));
          return (void**)p;
      }
      
      int main(void) {
          printf("%p\n", foo2());
          return 0;
      }
      

      结果是(在我的32-bit平台上):

      0x80e9008
      

      【讨论】:

      • return (void**)p 是不可移植的; void** 不是像 void* 这样的通用指针类型。它应该只返回void*,然后调用代码可以在取消引用之前将其转换为int **
      • @MattMcNabb: "return (void**)p is non-portable",你能举一些例子吗?真不知道这个问题,非常感谢!
      • int *void * 可能有不同的大小和表示形式(在现代系统上它们没有,但我已经使用了它),所以假装指针指向 @987654332 @ 当它实际指向 int * 时,将不会检索到合理的指针
      猜你喜欢
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      相关资源
      最近更新 更多