从C中的函数返回两个指针 [英] Return two pointers from a function in c

查看:178
本文介绍了从C中的函数返回两个指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以通过执行以下操作将指针返回到c中数组的第一个元素:

I know that you can return a pointer to the first element of an array in c by doing:

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

int *my_func(void);

int main(void)
{
    int *a;
    int i;

    a = my_func();

    for(i = 0; i < 3; i++)
    {
        printf("a[%d] = %d\n", i, a[i]);
    }

    free(a);

    return 0;
}

int *my_func(void)
{
    int *array;
    int i;

    array = calloc(3, sizeof(int));

    for(i = 0; i < 3; i++)
    {
        array[i] = i;
    }

    return array;
}

但是,如果我想返回两个指针而不是一个,我尝试了:

But if I wanted to return two pointers instead of just one, I tried:

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

int *my_func(int *);

int main(void)
{
    int *a;
    int *b;
    int i;

    a = my_func(b);

    for(i = 0; i < 3; i++)
    {
        printf("a[%d] = %d\n", i, a[i]);
        printf("b[%d] = %d\n", i, b[i]);
    }

    free(a);
    free(b);

    return 0;
}

int *my_func(int *array2)
{
    int *array;
    int i;

    array = calloc(3, sizeof(int));
    array2 = calloc(3, sizeof(int));

    for(i = 0; i < 3; i++)
    {
        array[i] = i;
        array2[i] = i;
    }

    return array;
}

但是此段在b的printf上出错.我通过valgrind进行了测试,并说在printf上对于b的大小为4无效,这意味着我正在用b指针做一些麻烦的事情.我只是想知道在c中返回"数组的第二个指针的最佳方法是什么?我问是因为我想这样做而不是使用全局变量(我不反对它们,因为它们有时很有用,我只是尽可能不使用它们).我在该站点上看到的其他问题都使用了静态分配的数组,但是我还没有偶然发现过使用动态分配的问题.提前致谢!

But this seg faults on the printf for b. I ran it through valgrind and it says that there is an invalid read of size 4 at the printf for b, which means I'm doing something screwy with the b pointer. I was just wondering what the best way to "return" a second pointer to an array was in c? I'm asking because I would like to do it this way rather than use a global variable (I'm not opposed to them, as they are useful at times, I just prefer not to use them if possible). The other questions I've seen on this site used statically allocated arrays, but I haven't yet stumbled across a question that was using dynamic allocation. Thanks in advance!

推荐答案

我可以想到三种(半)简单的方法来从C函数返回任何种类的多个项目.方式说明如下.它看起来像很多文本,但是这个答案主要是代码,因此请继续阅读:

I can think of three (and a half) simple ways to return multiple items of any kind from a C-function. The ways are described below. It looks like a lot of text, but this answer is mostly code, so read on:

  1. 按照您的要求传递输出参数,但请正确执行.您必须为main()中的int *分配空间,然后将指向该指针的指针传递给my_func.

  1. Pass in an output argument as you have done, but do it correctly. You have to allocate space for an int * in main() and then pass the pointer to that to my_func.

main()中:

a = my_func(&b);

my_func()变为:

int *my_func(int **array2)
{
    int *array;
    int i;

    array = calloc(3, sizeof(int));
    *array2 = calloc(3, sizeof(int));

    for(i = 0; i < 3; i++)
    {
        array[i] = i;
        (*array2)[i] = i;
    }

    return array;
}

  • 使函数分配两个指针的数组,这些指针指向您要分配的int数组.这将需要额外分配两个int指针,但这可能值得一试.

  • Make your function allocate array of two pointers to the int arrays you are trying to allocate. This will require an additional allocation of two int pointers, but may be worth the trouble.

    main()然后变为:

    int main(void)
    {
        int **ab;
        int i;
    
        ab = my_func(b);
    
        for(i = 0; i < 3; i++)
        {
            printf("a[%d] = %d\n", i, ab[0][i]);
            printf("b[%d] = %d\n", i, ab[1][i]);
        }
    
        free(ab[0]);
        free(ab[1]);
        free(ab);
    
        return 0;
    }
    

    my_func()然后变为:

    int **my_func(void)
    {
        int **arrays;
        int i, j;
    
        arrays = calloc(2, sizeof(int *));
        arrays[0] = calloc(3, sizeof(int));
        arrays[1] = calloc(3, sizeof(int));
    
        for(j = 0; j < 2; j++)
        {
            for(i = 0; i < 3; i++)
            {
                arrays[j][i] = i;
            }
        }
    
        return arrays;
    }
    

  • 返回结构或结构指针.您将需要定义结构,并决定是要返回结构本身,新分配的指向该结构的指针还是将其作为指针传递并让my_func()替您填充.

  • Return a structure or structure pointer. You will need to define the structure, and decide whether you want to return the structure itself, a newly allocated pointer to it, or pass it in as a pointer and have my_func() fill it in for you.

    结构定义看起来像这样:

    The structure definition would look something like this:

    struct x
    {
        int *a;
        int *b;
    }
    

    然后,将您的当前功能改写为以下三个选项之一:

    You would then rephrase your current functions as one of the following three options:

    1. 直接传递结构(一般不建议使用):

    1. Direct passing of structure (not recommended for general use):

    int main(void)
    {
        struct x ab;
        int i;
    
        ab = my_func();
    
        for(i = 0; i < 3; i++)
        {
            printf("a[%d] = %d\n", i, ab.a[i]);
            printf("b[%d] = %d\n", i, ab.b[i]);
        }
    
        free(ab.a);
        free(ab.b);
    
        return 0;
    }
    
    struct x my_func(void)
    {
        struct x ab;
        int i;
    
        ab.a = calloc(3, sizeof(int));
        ab.b = calloc(3, sizeof(int));
    
        for(i = 0; i < 3; i++)
        {
            ab.a[i] = i;
            ab.b[i] = i;
        }
    
        return ab;
    }
    

  • 返回一个指向动态分配结构的指针(通常,这是一个很好的选择):

  • Return a pointer to a dynamically allocated structure (this is a pretty good option in general):

    int main(void)
    {
        struct x *ab;
        int i;
    
        ab = my_func();
    
        for(i = 0; i < 3; i++)
        {
            printf("a[%d] = %d\n", i, ab->a[i]);
            printf("b[%d] = %d\n", i, ab->b[i]);
        }
    
        free(ab->a);
        free(ab->b);
        free(ab);
    
        return 0;
    }
    
    struct x *my_func(void)
    {
        struct x *ab;
        int i;
    
        ab = malloc(sizeof(struct x));
        ab->a = calloc(3, sizeof(int));
        ab->b = calloc(3, sizeof(int));
    
        for(i = 0; i < 3; i++)
        {
            ab->a[i] = i;
            ab->b[i] = i;
        }
    
        return ab;
    }
    

  • main()中分配结构,并通过传入的指针将其填充在my_func中.此选项通常以以下方式使用:如果您传入NULL指针,则my_func将分配结构,否则它将返回您传入的内容.为简单起见,此处显示的my_func版本没有返回值:

  • Allocate the structure in main() and fill it in in my_func via a passed-in pointer. This option is often used in a way where my_func would allocate the structure if you pass in a NULL pointer, otherwise it would return whatever you passed in. The version of my_func shown here has no return value for simplicity:

    int main(void)
    {
        struct x ab;
        int i;
    
        my_func(&ab);
    
        for(i = 0; i < 3; i++)
        {
            printf("a[%d] = %d\n", i, ab.a[i]);
            printf("b[%d] = %d\n", i, ab.b[i]);
        }
    
        free(ab.a);
        free(ab.b);
    
        return 0;
    }
    
    void my_func(struct x *ab)
    {
        int i;
    
        ab->a = calloc(3, sizeof(int));
        ab->b = calloc(3, sizeof(int));
    
        for(i = 0; i < 3; i++)
        {
            ab->a[i] = i;
            ab->b[i] = i;
        }
    
        return;
    }
    

  • 对于此处显示的所有示例,请不要忘记更新文件顶部的my_func声明,尽管我相信如果您忘记了,任何合理的编译器都会提醒您.

    For all the examples shown here, don't forget to update the declaration of my_func at the top of the file, although I am sure any reasonable compiler will remind you if you forget.

    还请记住,这些只是我一下子从大脑中抽出来的三个选择.尽管它们可能涵盖您很快遇到的所有用例中的99%,但还有(可能很多)其他选择.

    Keep in mind also that these are just three options I pulled out of my brain at a moments notice. While they are likely to cover 99% of any use cases you may come up against any time soon, there are (probably lots of) other options out there.

    这篇关于从C中的函数返回两个指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆