简体   繁体   English

返回指向结构数组的指针

[英]Returning a pointer to an array of structs

Let's say I have to create an array of structs that is allocated on the heap and return a pointer that points to this array of structs. 假设我必须创建一个在堆上分配的结构数组,并返回指向此结构数组的指针。

typedef struct Pair {
    int x;
    int y;
} Pair;

Pair** foo(int n, int m, int length)
{
    Pair* arr = malloc(sizeof(*arr) * length);

    for (int i = 0; i < length; ++i) {
        arr[i].x = n++;
        arr[i].y = m++;
    }

    return &arr;
}

When I compile a program containing this function, it warns me that I am returning the address of a local variable. 当我编译包含此函数的程序时,它警告我我正在返回局部变量的地址。 I assume this is because the pointer is initialised within the function (ie on the stack), therefore it counts as a local variable. 我假设这是因为指针在函数内(即在堆栈上)初始化,因此它被计为局部变量。

When I compile it, ignoring this warning, and run it anyway, the program crashes when the returned pointer is accessed. 当我编译它,忽略这个警告,并且无论如何运行它时,程序在访问返回的指针时崩溃。

I have tried allocating the pointer dynamically: 我试过动态分配指针:

Pair** ptr = malloc(sizeof(**ptr));
ptr = &arr;
...
return ptr;

but the program still crashes when this pointer is accessed. 但是当访问此指针时程序仍然崩溃。 How can I create this array within a function and return a pointer to this array so that it can be safely accessed? 如何在函数中创建此数组并返回指向此数组的指针,以便可以安全地访问它?

This array is initialized on the stack but the pointer ( arr ) is a local variable, so the caller, main , cannot access it. 此数组在堆栈上初始化,但指针( arr )是局部变量,因此调用者main无法访问它。 You do not need to use the address of the pointer. 您不需要使用指针的地址。 You can access the array with the pointer itself. 您可以使用指针本身访问该数组。

Pair* foo(int n, int m, int length)
{
    Pair* arr = malloc(sizeof(*arr) * length);

    for (int i = 0; i < length; ++i) {
        arr[i].x = n++;
        arr[i].y = m++;
    }

    return arr;
}

If you want an array of structs, the code: 如果你想要一个结构数组 ,代码:

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

typedef struct {
    int x;
    int y;
} Pair;

static Pair* foo(int n, int m, int length)   {
    Pair* arr = malloc(sizeof(*arr) * length);
    for (int i = 0; i < length; ++i) {
        arr[i].x = n++;
        arr[i].y = m++;
    }
    return arr;
}

int main(void) {
    Pair *z = foo(111, 222, 3);
    for (int i = 0; i < 3; ++i) 
        printf("z[%d]= { %d, %d }\n", i, z[i].x, z[i].y);
    free(z);
    return 0;
}

gives the output: 给出输出:

z[0]= { 111, 222 }
z[1]= { 112, 223 }
z[2]= { 113, 224 }

If you want an pointer to an array of structs, you can change your function signature from Pair** to be Pair* . 如果需要指向结构数组的指针,可以将函数签名从Pair**更改为Pair*


If you still want an pointer to an array of pointers, then allocate memory for a Pair struct for each index of arr . 如果您仍然需要指向指针数组的指针,则为每个arr索引为Pair结构分配内存。

for(int i = 0; i < length; ++i){
    arr[i] = malloc(sizeof(Pair));
    ...
}

Instead of returning &arr , you can declare arr as 您可以将arr声明为,而不是返回&arr

Pair** arr = malloc(sizeof(Pair*) * length);

Because arr is a local variable, it will be free when foo end. 因为arr是一个局部变量,所以当foo结束时它将是自由的。 So you don't have access for arr after. 所以你以后没有arr访问权限。 To solve this you should declare array pointer in heap: 要解决这个问题,你应该在堆中声明数组指针:

     Pair** foo(int n, int m, int length)
     {
           Pair ** arr = (Pair**)malloc(sizeof(Pair*));
           *arr = malloc(sizeof(Pair) * length);

           for (int i = 0; i < length; ++i) {
               (*arr)[i].x = n++;
               (*arr)[i].y = m++;
           }

            return arr;
       }

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

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