从函数中返回数组/指针

时间:2022-02-25 09:51:19

I am trying to create a new integer array which is derived from a string of characters. For example :

我正在尝试创建一个新的整数数组,它是从字符串中派生出来的。例如:

char x[] = "12334 23845 32084";  

int y[] = { 12334, 23845, 32084 };

I am having trouble understanding how to return an array ( which I understand isn't possible ) from a function.

我无法理解如何从函数中返回数组(我理解这是不可能的)。

I originally tried :

我最初尝试:

/* Convert string of integers into int array. */
int * splitString( char string[], int n )
{
    int newArray[n];

    // CODE

    return ( newArray );
}

int main( void )
{
    int x[n] = splitString( string, n );

    return ( 0 );
}

I later learned that you can not do this.

后来我才知道你不能这么做。

How do pointers work in regards to functions?

指针在函数中是如何工作的?

Thank you.

谢谢你!

7 个解决方案

#1


17  

Typically, you require the caller to pass in the result array.

通常,需要调用者传入结果数组。

void splitString( const char string[], int result[], int n) {
    //....
}

This is advantageous because the caller can allocate that memory wherever they want.

这是有利的,因为调用者可以将内存分配到他们想要的任何地方。

#2


12  

The problem is you're returning a pointer to something on the stack. You need to create your array on the heap, then free it when you're done:

问题是你返回一个指向堆栈上某个东西的指针。您需要在堆上创建数组,然后在完成时释放它:

int * splitString( char string[], int n )
{
    int *newArray = malloc(sizeof(int) * n);

    // CODE

    return ( newArray );
}

int main( void )
{
    int *x = splitString( string, n );

    // use it

    free(x);

    return ( 0 );
}

#3


5  

int * splitString( char string[], int n )
{
    int newArray[n];
    return ( newArray );
}

This is very bad! The array newArray local to the function gets destroyed when the function returns. You'd be left out with a dangling pointer and using it would invoke undefined behaviour.

这是非常糟糕的!当函数返回时,函数的本地数组newArray将被销毁。如果使用悬浮指针,则会调用未定义的行为。

You can't return an array from a function. The best you can do is

不能从函数返回数组。你能做的最好的就是

int * splitString( char string[], int n )
{
    int *newArray = malloc(n*sizeof(int)); // the array gets allocated on the heap rather than on the stack(1)
    // Code 
    return ( newArray );
}

Don't forget to free the allocated memory.

不要忘记释放分配的内存。

(1) Note that the standard doesn't use/define the term stack or heap as such.

(1)注意,标准不使用/定义术语堆栈或堆。

#4


3  

Rather than returning an array with return (newArray), you return a pointer to the first element of newArray.

不是返回带有return (newArray)的数组,而是返回指向newArray的第一个元素的指针。

The problem is that you're allocating the array the wrong way. If you instantiate it with int newArray[n], memory gets allocated on the current stack frame. That memory will be freed as soon as your function returns, and whatever was in the array will be garbage. Instead, do the following:

问题是你用错误的方式分配数组。如果用int newArray[n]实例化它,那么内存就会在当前堆栈帧上分配。当函数返回时,该内存将被释放,数组中的任何内容都将成为垃圾。相反,请执行以下操作:

int *newArray = malloc(n * sizeof(int));
// etc.
return newArray

By using malloc, you allocate memory on the heap, where it will survive past the end of the current stack frame. Just remember to free(newArray) somewhere in your program when you're done.

通过使用malloc,您可以在堆上分配内存,它将在当前堆栈帧结束后继续存在。当你完成后,记得在程序的某个地方释放(newArray)。

#5


2  

You can wrap an array in a structure and then return an instance of the structure. I'm mentioning this for completeness, it's not really something you'd want to do as it's ugly and there are better alternatives.

可以将数组封装到结构中,然后返回结构的实例。我说的是为了完整性,这不是你想做的,因为它很丑,还有更好的选择。

#include <stdio.h>

struct retval
{
    int a[10];
};

struct retval test()
{
    struct retval v = {{1, 5, 6}};
    return v;
}

int main()
{
    struct retval data = test();
    printf("%d %d\n", data.a[1], data.a[2]);
}

#6


2  

I would go about it this way

我会这样做

/* Convert string of integers into int array. */
void splitString(char string[], int *out_arr, int n )
{

    // code that fills each cell of out_arr

}

int main( void )
{
    int x[n];
    splitString( string,(int *)x, n );

    return ( 0 );
}

#7


1  

Of course it's possible. This is the way I prefer: int func(int** results)

当然这是有可能的。这是我喜欢的方式:int func(int** results)

Function returns number of elements in results. results is a pointer to an int array.

函数返回结果中的元素个数。结果是指向int数组的指针。

#1


17  

Typically, you require the caller to pass in the result array.

通常,需要调用者传入结果数组。

void splitString( const char string[], int result[], int n) {
    //....
}

This is advantageous because the caller can allocate that memory wherever they want.

这是有利的,因为调用者可以将内存分配到他们想要的任何地方。

#2


12  

The problem is you're returning a pointer to something on the stack. You need to create your array on the heap, then free it when you're done:

问题是你返回一个指向堆栈上某个东西的指针。您需要在堆上创建数组,然后在完成时释放它:

int * splitString( char string[], int n )
{
    int *newArray = malloc(sizeof(int) * n);

    // CODE

    return ( newArray );
}

int main( void )
{
    int *x = splitString( string, n );

    // use it

    free(x);

    return ( 0 );
}

#3


5  

int * splitString( char string[], int n )
{
    int newArray[n];
    return ( newArray );
}

This is very bad! The array newArray local to the function gets destroyed when the function returns. You'd be left out with a dangling pointer and using it would invoke undefined behaviour.

这是非常糟糕的!当函数返回时,函数的本地数组newArray将被销毁。如果使用悬浮指针,则会调用未定义的行为。

You can't return an array from a function. The best you can do is

不能从函数返回数组。你能做的最好的就是

int * splitString( char string[], int n )
{
    int *newArray = malloc(n*sizeof(int)); // the array gets allocated on the heap rather than on the stack(1)
    // Code 
    return ( newArray );
}

Don't forget to free the allocated memory.

不要忘记释放分配的内存。

(1) Note that the standard doesn't use/define the term stack or heap as such.

(1)注意,标准不使用/定义术语堆栈或堆。

#4


3  

Rather than returning an array with return (newArray), you return a pointer to the first element of newArray.

不是返回带有return (newArray)的数组,而是返回指向newArray的第一个元素的指针。

The problem is that you're allocating the array the wrong way. If you instantiate it with int newArray[n], memory gets allocated on the current stack frame. That memory will be freed as soon as your function returns, and whatever was in the array will be garbage. Instead, do the following:

问题是你用错误的方式分配数组。如果用int newArray[n]实例化它,那么内存就会在当前堆栈帧上分配。当函数返回时,该内存将被释放,数组中的任何内容都将成为垃圾。相反,请执行以下操作:

int *newArray = malloc(n * sizeof(int));
// etc.
return newArray

By using malloc, you allocate memory on the heap, where it will survive past the end of the current stack frame. Just remember to free(newArray) somewhere in your program when you're done.

通过使用malloc,您可以在堆上分配内存,它将在当前堆栈帧结束后继续存在。当你完成后,记得在程序的某个地方释放(newArray)。

#5


2  

You can wrap an array in a structure and then return an instance of the structure. I'm mentioning this for completeness, it's not really something you'd want to do as it's ugly and there are better alternatives.

可以将数组封装到结构中,然后返回结构的实例。我说的是为了完整性,这不是你想做的,因为它很丑,还有更好的选择。

#include <stdio.h>

struct retval
{
    int a[10];
};

struct retval test()
{
    struct retval v = {{1, 5, 6}};
    return v;
}

int main()
{
    struct retval data = test();
    printf("%d %d\n", data.a[1], data.a[2]);
}

#6


2  

I would go about it this way

我会这样做

/* Convert string of integers into int array. */
void splitString(char string[], int *out_arr, int n )
{

    // code that fills each cell of out_arr

}

int main( void )
{
    int x[n];
    splitString( string,(int *)x, n );

    return ( 0 );
}

#7


1  

Of course it's possible. This is the way I prefer: int func(int** results)

当然这是有可能的。这是我喜欢的方式:int func(int** results)

Function returns number of elements in results. results is a pointer to an int array.

函数返回结果中的元素个数。结果是指向int数组的指针。