简体   繁体   English

你能在函数中返回一种指向指针的指针吗?

[英]Can you return a type of pointer to pointer in a function?

I know you can return a type of pointer from a function. 我知道你可以从函数返回一种指针。 ex. 恩。 void *foo() Can you return a type of pointer to pointer in a function? void *foo()你能在函数中返回一种指向指针的指针吗? ex. 恩。 void **foo2()

Here is more info about my question: 以下是有关我的问题的更多信息:

I try to assign a ptr-to-ptr, tmp2, to blocks[i][j], and then return tmp2. 我尝试将ptr-to-ptr,tmp2分配给blocks [i] [j],然后返回tmp2。 blocks[i][j] is a ptr-to-ptr as well. blocks [i] [j]也是ptr-to-ptr。

I'm confused to manipulate a ptr to a ptr-to-ptr: I am not sure if return ((tmp2+i)+j); 我很难操纵ptr到ptr-to-ptr:我不确定是否return ((tmp2+i)+j); is the cause of the segmentation fault at line printf("2---%d\\n", **tmpPtr2); printf("2---%d\\n", **tmpPtr2);的分段错误的原因printf("2---%d\\n", **tmpPtr2); . To debug, I try to print: printf("%d\\n", *( (*(tmp2+i)) +j) ); 为了调试,我尝试打印: printf("%d\\n", *( (*(tmp2+i)) +j) ); However, it causes a new segmentation fault. 但是,它会导致新的分段错误。

#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;
}

Update to answer my question: 更新回答我的问题:

(1) Adding t tmp2=blocks; (1)添加t tmp2=blocks; to the top of findBlock2() removed both segfaults. 到findBlock2()的顶部删除了两个段错误。

(2) return ((tmp2+i)+j); (2) return ((tmp2+i)+j); shows how to manipulate a ptr-to-ptr pointing to a ptr-to-ptr or a 2D array 展示了如何操作指向ptr-to-ptr或2D数组的ptr-to-ptr

(3) printf("%d\\n", *( (*(tmp2+i)) +j) ); (3) printf("%d\\n", *( (*(tmp2+i)) +j) ); shows how to do (2) and dereference it. 显示如何做(2)并取消引用它。

Hope it helps others 希望它能帮助别人

Yeah, just like you would with any pointer variables. 是的,就像你使用任何指针变量一样。

#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;
}

Adding to the other part. 添加到其他部分。 In your function findBlock2 besides accessing an invalid reference that has already been pointed out, it seems that your objective is to return a reference to the block that fulfills if statement. 在您的函数findBlock2除了访问已经指出的无效引用之外,您的目标似乎是返回对满足if语句的块的引用。 If that is the case then returning a pointer to int* should suffice. 如果是这种情况,那么返回指向int*的指针就足够了。

int *findBlock2( int b )

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

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

The answer is "yes". 答案是肯定的。 Please refer the following code: 请参考以下代码:

#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;
}

The result is (in my 32-bit platform): 结果是(在我的32-bit平台中):

0x80e9008

You probably want a 2D array at not some slow, fragmented lookup table. 您可能想要一个二维数组,而不是一些缓慢,碎片化的查找表。 In that case, do like this: 在这种情况下,请这样做:

#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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM