C语言 从函数返回整数数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16604631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:22:18  来源:igfitidea点击:

Return integer array from function

c

提问by seasick

I'm trying to return an array of integers from a function, sort the numbers then pass everything back to main. I haven't allocated and freed memory in this piece of code. I was just trying to see if it would actually work. The compiler flags an error for the statement b=sort(a). It says that it is not assignable, which would make sense. The input integers are not pointers. Is there a way to declare an array of integers as pointers? such as :

我试图从函数返回一个整数数组,对数字进行排序,然后将所有内容传回 main。我在这段代码中没有分配和释放内存。我只是想看看它是否真的有效。编译器为语句标记错误b=sort(a)。它说它不可分配,这是有道理的。输入整数不是指针。有没有办法将整数数组声明为指针?如 :

int *a[5]={3,4}

int *a[5]={3,4}

#include <stdio.h>
#include <stdlib.h>
int *sort(int *input_array);

int *sort(int *input_array)
{
    return input_array;
}

int main()
{
    int a[5]={3,4};
    int b[5];
    b=sort(a);
    return 0;
}

回答by aaaaaa123456789

When you create an array, you cannot assign to the array itself (only to the elements). Besides, since when you pass an array, you're passing it by reference, sort()would modify the array, making it unneeded to return it.

创建数组时,不能分配给数组本身(只能分配给元素)。此外,因为当你传递一个数组时,你是通过引用传递它,sort()会修改数组,使它不需要返回它。

What you're looking for is either of: sorting the original array, which would be like this:

您正在寻找的是:对原始数组进行排序,如下所示:

void sort (int * array);

void sort (int * array) {
  // do stuff on the array
}

int main (void) {
  int a[5] = {1, 46, 52, -2, 33};
  sort(a); // result is still in a
  return 0;
}

Or creating a copy and sorting it, which would be like this:

或者创建一个副本并对其进行排序,就像这样:

#include <stdlib.h>
#include <string.h>
int * sort (int * array, unsigned size);

int * sort (int * array, unsigned size) {
  int * copy = malloc(sizeof(int) * size);
  memcpy(copy, array, size * sizeof(int));
  // sort it somehow
  return copy;
}

int main (void) {
  int a[5] = {1, 46, 52, -2, 33};
  int * b; // pointer because I need to assign to the pointer itself
  b = sort(a, (sizeof a) / (sizeof *a)); // now result is in b, a is unchanged
  // do something with b
  free(b); // you have to
  return 0;
}

回答by unwind

You can't assign arrays, they're not "first class citizens" but instead behave much like pointers.

你不能分配数组,它们不是“一等公民”,而是表现得很像指针。

You need something like:

你需要这样的东西:

int a[] = { 3, 4 };
int *b;

b = sort(a, sizeof a / sizeof *a);

The sizeofexpression is needed to compute the length of the array, the sort()function can't determine that from the bare pointer it gets passed.

sizeof需要表达式,来计算所述阵列的长度,sort()功能不能确定从裸指针它被传递。

UPDATE: The above assumes that you won'tbe changing the input array, but if you do then (as pointed out in a comment, thanks) the return value is of course not needed since the caller's awill have changed when the sort()call returns.

更新:以上假设您不会更改输入数组,但是如果您这样做了(如评论中所指出的,谢谢)当然不需要返回值,因为调用者a将在sort()调用返回时更改。

回答by Rohan

If you are passing array - a pointer of int, you don't need to return a changed array. The array that you passed will get changed.

如果您正在传递数组 - 的指针int,则无需返回更改后的数组。您传递的数组将被更改。

As @unwind suggested, you should pass number of elements to the function also so that the function knows how many elements are there in the array.

正如@unwind 建议的那样,您还应该将元素数量传递给函数,以便函数知道数组中有多少个元素。

回答by xaxxon

You can't return an array of anything in C. You can only return a single instance of a single datatype.

您不能在 C 中返回任何数组。您只能返回单个数据类型的单个实例。

That datatype can be a pointer to memory storing a sequential list of numbers (or anything else), but you lose all information about how long the result is, so you either need to know that, or you have to have another value as an output variable to tell you the length.

该数据类型可以是指向存储数字(或其他任何内容)顺序列表的内存的指针,但是您会丢失有关结果多长时间的所有信息,因此您要么需要知道这一点,要么必须有另一个值作为输出变量告诉你长度。

You can also return a custom datatype, such as a struct, that would contain both the list of data as well as the length. However, returning a large datastructure creates multiple shallow copies of the data structure, slowing down execution of your program as well as creating memory nightmares with leaks and multiple references.

您还可以返回自定义数据类型,例如结构体,它既包含数据列表又包含长度。但是,返回大型数据结构会创建数据结构的多个浅表副本,从而减慢程序的执行速度并造成内存泄漏和多次引用的噩梦。

Returning a pointer to a custom data structure, however, can work very well.

然而,返回一个指向自定义数据结构的指针可以很好地工作。