问题描述
我设法赢得成功在 C
有变长数组工作,我现在有以下几点:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT(*富(为size_t行,为size_t COL))[3];诠释主要(无效){
为size_t行,列;
的printf(给ROW:);
如果(scanf的(%つ,&放大器;!行)= 1){
的printf(错误,scanf函数ROW \\ n);
出口(1);
} 的printf(给COL:);
如果(scanf函数(%俎,&安培;!COL)= 1){
的printf(错误,scanf函数COL \\ n);
出口(2);
} INT(* ARR)[COL] = foo的(行,列); 用于(为size_t我= 0; I<排;我++){
用于(为size_t J = 0; J<西; J ++){
的printf(%d个*(*(ARR + I)+ J));
}
} 免费(ARR);
}
INT(*富(为size_t行,为size_t COL))[3] {
INT(* ARR)[COL] =的malloc(行* COL *的sizeof(INT));
int类型l = 0; 如果(ARR == NULL){
的printf(错误,malloc的\\ n);
出口(3);
} 用于(为size_t我= 0; I<排;我++){
用于(为size_t J = 0; J<西; J ++){
*(*(ARR + I)+ J)= 1;
升++;
}
} 返回ARR;
}
输出:
给该行:2
给COL:5
0 1 2 3 4 5 6 7 8 9
现在这样:
INT(*富(为size_t行,为size_t COL))[3] {/ * code * /}
意味着,如果我明白,返回一个指向一个int数组3右声明富与两个参数(为size_t行,为size_t COL)的功能。
我不完全能够理解这种功能,它是更复杂,我现在变长数组时的大小只在运行时知道,但我觉得这是一件好事。我与 C11
标准只工作。
任何方式在这里 INT(*富(为size_t行,为size_t COL))[3]
我有这样的[3]这我不清楚它是如何工作我怎样才能使人们有可能在运行时(当然前提是可能的),像 INT(*富(为size_t行,为size_t COL))[SIZE]
。
我读 C
一些书,但没有对这种情况的解释准确和谷歌也没有帮助,所以我有两个问题:
1)本posiible INT(*富(为size_t行,为size_t COL))[SIZE]
,其中大小必须参数?或者我应该声明此功能的另一种方式?
2),这是什么我想在这里以正确的方式,或者有另一种选择?
我只是想返回一个指向一个数组,其中大小知道在运行时,不编译时间。 的malloc
和免费
的通话只发生一次,这是一个很好的方法,因为每当的malloc
被调用时,我们的程序干扰内核分配内存和标记页面writable.So这种方法有较少的头顶上内核
。
它可以写在一个的malloc
与VLA的工作。
编辑:
@ChronoKitsune说,我应该使用/尝试 []
(未指定大小的数组)在这种情况下功能将变成这个样子:
INT(*富(为size_t行,为size_t COL))[] {/ * code * /}
这是我应该用什么?
Type declarations are most easily read from inside out. I'll start simple, and build up to your example. If you write
int foo[3];
or
int (foo)[3];
you are declaring foo
as an array of 3 int
s. The parentheses here serve a precedence-directing grouping function, just like in expressions, but they are unnecessary because the type is interpreted the same way in both cases.
If you write
int (*foo)[3];
you are declaring foo
to be a pointer to an array of 3 int
s. Equivalently, you can read that as saying that the thing foo
points to is an array of 3 int
s, with it being implicit that foo
is a pointer. In this case the parentheses are necessary; without them you would be declaring foo
as an array of 3 int *
.
If you write
int (*foo(size_t row, size_t col))[3];
you are declaring that foo
is a function taking two arguments of type size_t
, whose return value points to an array of 3 int
s.
If SIZE
is not a compile-time constant then you cannot do that. C2011 holds that
(6.7.6.2/2)
In other words, because all functions have file scope and either internal or external linkage, function return types cannot be VLAs, pointers to VLAs, or any such type (these are "variably-modified" types).
Generally, in such cases one returns a pointer to the first element of the array rather than a pointer to the whole array. The pointed-to address is the same either way, but the type is different:
int *foo(size_t row, size_t col);
The return type does not carry information about the length of the pointed-to array, but if C allowed functions to return variably-modified types then that would rely anyway on a mechanism by which the code could know the variable dimensions in any particular context. In other words, if you don't know the expected array length independent of the function's return type, then there's no way you could have used a variably-modified return type anyway.
If indeed you need the function to return both a pointer to a runtime-determined number of elements and also the number of elements, then you can return a struct containing both, or you can return one or both values via pointer parameters.
Update:
It is also possible to declare your function to return a pointer to an array of unspecified size, as @ChronoKitsune suggested. The syntax would be
int (*bar(size_t row, size_t col))[];
, but you will find that return type harder to use in almost every way. For example, it is trickier and uglier to declare variables that can hold the return value:
int (*array_ptr)[] = bar(x, y);
and to access elements of the pointed-to array:
int z = (*array_ptr)[1];
Contrast that with
int *ptr = foo(x, y);
int w = ptr[2];
这篇关于函数返回一个指向数组的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!