C程序返回数组 [英] C program returning array

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

问题描述

我工作的一个非常基本的程序,我想长度为2的整数数组回到我的主块。我不能让它工作,虽然,我被告知我可能需要指针做到这一点。如何指针工作,我怎么能在我的程序中使用呢?
这是我目前的code:

  INT [] RETURN2();
诠释主(){  诠释一个[2];  一个=请求();
  的printf(%D \\ n,一个[0],A [1]);
  返回(0);
}INT []请求()
{
  诠释一个[2];  一个[0] = -1;
  一个[1] = 8;  返回;
}


解决方案

  • 您不能声明函数返回一个数组。


      

    ISO / IEC 9899:1999


      
      

    §6.9.1函数定义


      
      

    ¶3函数的返回类型应为无效或比数组类型以外的对象类型。


    C2011会说,本质上是一回事。


  • 您不应该永远返回一个指针(非静态)局部变量从一个功能,因为它已不再是在范围(因此无效)尽快返回完成


您可以返回一个指向数组的开始如果阵列是静态分配的,或者如果它通过动态分配的malloc()

 为int *功能1(无效)
{
    静态int类型的[2] = {-1,+ 1};
    返回;
}静态INT B〔2] = {-1,+ 1};为int *函数2(无效)
{
    返回b;
}/ *调用者必须释放由function3()返回的指针* /
为int * function3(无效)
{
    INT * C =的malloc(2 * sizeof的(* C));
    C [0] = 1;
    C [1] = + 1;
    返回℃;
}

或者,如果你喜欢冒险,你可以返回一个指向数组:

  / *调用者必须释放由function4()返回的指针* /
INT(* function4(无效))[2]
{
    INT(* D)[2] =的malloc(sizeof的(* D));
    (* D)[0] = 1;
    (* D)[1] = + 1;
    返回D组;
}

小心那个函数声明!它并不需要太多的变化完全改变它的意义:

  INT(* function4(无效))[2]; //函数返回指向两个int数组
INT(* function5 [2])(无效); //两个指针数组来返回int功能
INT(* function6(无效)[2]); //非法的:函数返回两个指针数组为int
为int *因数7(无效)[2]; //非法的:函数返回两个指针数组为int

I am working on a very basic program where I want to return an integer array of length 2 to my main block. I can't get it to work though, and I was told that I may need pointers to do this. How do pointers work, and how can I use this in my program? Here is my current code:

int[] return2();


int main() {

  int a[2];

  a = request();
  printf("%d%d\n", a[0], a[1]);


  return(0);
}

int[] request ()
{
  int a[2];

  a[0] = -1;
  a[1] = 8;

  return a;
}

解决方案

  • You can't declare a function returning an array.

    ISO/IEC 9899:1999

    §6.9.1 Function definitions

    ¶3 The return type of a function shall be void or an object type other than array type.

    C2011 will say essentially the same thing.

  • You shouldn't ever return a pointer to a (non-static) local variable from a function as it is no longer in scope (and therefore invalid) as soon as the return completes.

You can return a pointer to the start of an array if the array is statically allocated, or if it is dynamically allocated via malloc() et al.

int *function1(void)
{
    static int a[2] = { -1, +1 };
    return a;
}

static int b[2] = { -1, +1 };

int *function2(void)
{
    return b;
}

/* The caller must free the pointer returned by function3() */
int *function3(void)
{
    int *c = malloc(2 * sizeof(*c));
    c[0] = -1;
    c[1] = +1;
    return c;
}

Or, if you are feeling adventurous, you can return a pointer to an array:

/* The caller must free the pointer returned by function4() */
int (*function4(void))[2]
{
    int (*d)[2] = malloc(sizeof(*d));
    (*d)[0] = -1;
    (*d)[1] = +1;
    return d;
}

Be careful with that function declaration! It doesn't take much change to change its meaning entirely:

int (*function4(void))[2]; // Function returning pointer to array of two int
int (*function5[2])(void); // Array of two pointers to functions returning int
int (*function6(void)[2]); // Illegal: function returning array of two pointers to int
int  *function7(void)[2];  // Illegal: function returning array of two pointers to int

这篇关于C程序返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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