如何返回2D数组?
我正在寻找一种创建2D数组并返回的有效方法,通常我会制作一系列指针,为每个指针分配内存,然后返回** ptr。我一直在寻找另一种方法,因为那时我必须释放分配的每个记忆。
我在线阅读,您可以分配一个2D数组:int( *arr)[n] = malloc(sizeof *arr *i);
其中PTR是一个指针,指的是arr [n]
的地址。
当我尝试返回ARR时,从以下函数:int *array_of_smallest(int count);
我得到:警告:从不兼容的指针类型'int( *)[n]'[ - wincompatible-pointer-types]
的分配
给 'int *'然后返回它,我只返回一个1D数组。
我认为我正在混合不同的概念并使自己感到困惑。
有了一系列的指针,我没有问题,但是我想要一种更简单的方法来创建一个2D数组,也更容易释放,但是我无法从功能中返回该数组。
I was searching for an efficient way to create a 2D array and return it, usually I make an array of pointers, allocate memory for each pointer, and return an **ptr. I was looking for another way to do it because then I have to free every single memory allocated.
I was reading online that you can allocate a 2D array like this: int (*arr)[n] = malloc( sizeof *arr * i );
In which ptr is a pointer, pointing to the adress of arr[n]
.
When I try to return arr, from the following function: int *array_of_smallest(int count);
I get: warning: assignment to ‘int *’ from incompatible pointer type ‘int (*)[n]’ [-Wincompatible-pointer-types]
If I initialise another pointer and point it to array, then return it, I'm only returning a 1D array.
I think I'm mixing diferent concepts and confusing myself.
With an array of pointers I don't have a problem, but I wanted a simplier way to create a 2D array, also easier to free, but I'm not being able to return that array from my function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您声明了变量修改类型int( *)[n]的指针。
那是声明器中使用的变量
n
不是整数常数表达式。在这种情况下,函数应具有返回类型
void *
。可以像在呼叫者中一样声明,您将
在此声明中编写
n
的值必须与函数中使用的值相同。如果要使用类似的整数常数表达式
,则该函数声明看起来像是这样
,或者您可以在函数声明之前引入typedef名称
,在这种情况下,该函数看起来像
You declared a pointer of the variable modified type type int ( * )[n].
That is the variable
n
used in the declarator is not an integer constant expression.In this case the function should have the return type
void *
. And it can be declared likeIn the caller you will write
In this declaration the value of
n
must be the same as used within the function.If to use an integer constant expression like
then the function declaration will look like
Or you can introduce a typedef name before the function declaration like
and in this case the function will look like
返回数组的基本问题是,在文件范围内不可能声明可变修饰的类型(例如指向VLA的指针)。
问题的原因可以解释为如下。您的代码的行为或多或少类似:
不幸的是,由于两个原因,定义这种类型
t
是不可能的:n
通常在文件范围内看不见,除非它是n
的全局变量workarounds
return
void*
如其他< a href =“ https://stackoverflow.com/a/72516342/4989451”>答案。将指针传递给“副参考”样式的VLA,作为指向数组指针的指针
int []
。不可能返回不完整的类型,但是可以将指针返回到不完整类型的指针:或在
type> typeof
Extension(C23中的功能)的帮助下有点清洁器,并将其如
void*案例:
此解决方案为元素类型提供类型检查,但是它仅适用于二维数组。
另外,请使用
typeof
扩展名来推断VLA视图的类型,而不是在宏中声明它:The fundamental issue with returning the array is that it is not possible to declare Variably-Modified Type (like a pointer to VLA) at the file scope.
The reason of the problem can be explained as follow. Your code behaves more or less like this:
Unfortunately, defining such a type
T
is not possible because of two reasons:n
is usually not visible at file scope, except if it was a global variablen
would require code execution at file scope which is forbiddenWorkarounds
Return
void*
as described in other answer.Pass a pointer to VLA in "by-reference" style, as a pointer to a pointer to array
int[]
. It is not possible to return incomplete types but it is possible to return a pointer to incomplete types:or a bit cleaner with a help of
typeof
extension (feature in C23)And use it as in the
void*
case:This solution provides type-checking for the element type, however it works only for 2-dimensional arrays.
Alternatively, use
typeof
extension to infer the type of VLA view rather than declare it in a macro: