Return integer array from function

I am trying to return an array of integers from a function, sort the numbers, and then pass everything back to the main one. I did not allocate or free memory in this piece of code. I was just trying to figure out if this would really work. The compiler puts an error for the operator b=sort(a) . It says that it cannot be appointed, which makes sense. Input integers are not pointers. Is there a way to declare an array of integers as pointers? eg:

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; } 
+6
source share
4 answers

When you create an array, you cannot assign to the array itself (only for elements). In addition, since when you pass an array, you pass it by reference, sort() will modify the array, making it unnecessary to return.

What you are looking for is either sorting the original array, which would look 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 create a copy and sort it, which will look 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; } 
+10
source

You cannot assign arrays, they are not "first class citizens", but instead behave like pointers.

You need something like:

 int a[] = { 3, 4 }; int *b; b = sort(a, sizeof a / sizeof *a); 

To calculate the length of an array, the sizeof expression is required; the sort() function cannot determine which of the bare pointers it passed.

UPDATE The above assumes that you will not modify the input array, but if you do (as indicated in the comment, thanks), the return value is of course not required, since the calling a will be changed when the sort() call returns.

+5
source

If you pass an array - an int pointer, you do not need to return the modified array. The array you passed in will be modified.

As suggested by @unwind, you must pass the number of elements to the function as well so that the function knows how many elements are in the array.

+1
source

You cannot return an array from C. You can only return one instance of the same data type.

This data type may be a pointer to memory storing a sequential list of numbers (or something else), but you lose all the information about how long the result is, so you either need to know this, or you should have a different value as output variable to indicate the length.

You can also return your own data type, for example, struct, which will contain both a list of data and a length. However, returning a large data structure creates several small copies of the data structure, slowing down your program and also creating memory nightmares with leaks and multiple links.

Reverting a pointer to a custom data structure, however, may work very well.

+1
source

Source: https://habr.com/ru/post/945241/


All Articles