简体   繁体   English

function 的数组指针类型返回

[英]array pointer type for function return

I started using array pointers a lot recently because it simplify a lot the allocation/deallocation, but the type always throw an error by the compiler when its the return type of a function.我最近开始大量使用数组指针,因为它大大简化了分配/释放,但是当它的返回类型为 function 时,编译器总是会抛出错误。 this is accepted:这是被接受的:

int (*arr_ptr)[SIZE] = NULL;

but this is not:但这不是:

int (*)[] foo(void) {
    return NULL;
}

the compiler throw this error:编译器抛出此错误:

src/main.c:51:7: error: expected identifier or ‘(’ before ‘)’ token
   51 | int (*)[] foo(void) {
      |       ^

For the moment I just typedef the type and it work:目前我只是 typedef 类型并且它工作:

typedef int(*tdarr_ptr_int)[];
tdarr_ptr_int foo(void) {
    return NULL;
}

But this not a suitable longterm solution for me, how can I specify this return type without having to typedef it?但这对我来说不是一个合适的长期解决方案,我怎样才能指定这个返回类型而不必 typedef 呢?

C uses infix notation, as we saw from your first example: C 使用中缀表示法,正如我们从您的第一个示例中看到的那样:

int (*arr_ptr)[SIZE] = NULL;

is correct whereas是正确的,而

int (*)[SIZE] arr_ptr = NULL;

is not.不是。 You can fix the function version by applying the same principle:您可以通过应用相同的原理修复 function 版本:

int (*foo(void))[] {

NB.注意。 People sometimes criticize the infix, saying that postfix is easier, but hopefully it is clear from this example that the only difference is the placement of the identifier, and in fact having the identifier in the infix position helps you see where the middle is:)人们有时会批评中缀,说后缀更容易,但希望从这个示例中可以清楚地看出,唯一的区别是标识符的位置,事实上,中缀 position 中的标识符可以帮助您了解中间在哪里:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM