简体   繁体   English

将指针返回到数组的函数

[英]Function returning a Pointer to an Array

I managed to succed to work with Variable length Arrays in C and I have now the following: 我成功地使用了C可变长度数组,我现在有以下内容:

#include <stdio.h>
#include <stdlib.h>

int (*foo(size_t row, size_t col))[3];

int main(void){
    size_t row, col;


    printf("Give the ROW: ");
    if ( scanf("%zu",&row) != 1){
        printf("Error, scanf ROW\n");
        exit(1);
    }

    printf("Give the COL: ");
    if ( scanf("%zu",&col) != 1){
        printf("Error, scanf COL\n");
        exit(2);
    }

    int (*arr)[col] = foo(row, col);

    for ( size_t i = 0; i < row; i++){
        for( size_t j = 0; j < col; j++){
            printf("%d ",*(*(arr+i)+j));
        }
    }

    free(arr);
}


int (*foo(size_t row, size_t col))[3]{
    int (*arr)[col] = malloc(row * col * sizeof(int));
    int l=0;

    if (arr == NULL){
        printf("Error, malloc\n");
        exit(3);
    }

    for ( size_t i = 0; i < row; i++){
        for( size_t j = 0; j < col; j++){
            *(*(arr+i)+j) = l;
            l++;
        }
    }

    return arr;
}

Output: 输出:

Give the ROW: 2
Give the COL: 5
0 1 2 3 4 5 6 7 8 9

Now This: 现在这个:

int (*foo(size_t row, size_t col))[3]{ /* code */ }

means, if I understood right that declares foo as a function with two parameters (size_t row, size_t col) that returns a pointer to an array 3 of int. 意味着,如果我理解正确,将foo声明为具有两个参数(size_t row,size_t col)的函数,该参数返回指向int数组3的指针。

I'm not entirely capable to understand this kind of Function and it is more complicated for me now working with Variable length Arrays when the size is known only at run time, but I find this a good thing. 我并不完全能够理解这种函数,当我只在运行时知道大小时,使用可变长度数组对我来说更复杂,但我发现这是一件好事。 I am working only with C11 standard. 我只使用C11标准。

Any way here int (*foo(size_t row, size_t col))[3] I have this [3] which I'm not clear about how it works and how can I make it possible on run time (of course only if is possible), something like int (*foo(size_t row, size_t col))[SIZE] . 这里有任何方式int (*foo(size_t row, size_t col))[3]我有这个[3],我不清楚它是如何工作的,如何在运行时使它成为可能(当然只有可能),像int (*foo(size_t row, size_t col))[SIZE]

I read some books about C , but there is no exactly explanations about this situation and Google doesn't help either, so I have two Questions: 我读了一些关于C书,但是没有关于这种情况的确切解释,谷歌也没有帮助,所以我有两个问题:

1) Is this posiible int (*foo(size_t row, size_t col))[SIZE] , where SIZE has to be parameter? 1)这是可能的int (*foo(size_t row, size_t col))[SIZE] ,其中SIZE必须是参数? Or should i declare this Function in another way? 或者我应该以另一种方式声明这个功能?

2) is this the right way for what I tried here, or there is another alternative? 2)这是我在这里尝试过的正确方法,还是有另一种选择?

I'm only trying to return a pointer to an array where the size is know at run time and not compile time. 我只是试图返回一个指向一个数组的指针,该数组在运行时知道大小而不是编译时间。 The call of malloc and free happens only one time and this is a good approach, because whenever malloc is called, our program disturb kernel to allocate memory and mark page as writable.So this method have less over head on kernel . mallocfree的调用只发生一次,这是一个很好的方法,因为每当调用malloc ,我们的程序会干扰内核分配内存并将页面标记为可写。所以这种方法对kernel较小。 It can be written in one malloc working with VLA's 它可以用一个与VLA一起工作的malloc编写

EDIT: 编辑:

@ChronoKitsune said that i should use/try [] (array of unspecified size) in which case the function will become like this: @ChronoKitsune说我应该使用/ try [] (未指定大小的数组),在这种情况下函数将变成这样:

int (*foo(size_t row, size_t col))[]{ /* code */ }

Is this what i should use? 这是我应该使用的吗?

Any way here int (*foo(size_t row, size_t col))[3] I have this [3] which I'm not clear about how it works 这里有任何方式int (*foo(size_t row, size_t col))[3]我有这个[3] ,我不清楚它是如何工作的

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. 你将foo声明为3个int的数组。 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. 你宣称foo是一个指向 3 int数组的指针 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. 同样地,你可以读到这就是说foo指向的东西是一个3个int的数组,隐含foo是一个指针。 In this case the parentheses are necessary; 在这种情况下,括号是必要的; without them you would be declaring foo as an array of 3 int * . 如果没有它们,你会将foo声明为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. 你声明foo是一个带有两个size_t类型参数的函数,它的返回值指向一个3 int的数组。

and how can I make it possible on run time (of course only if is possible), something like int (*foo(size_t row, size_t col))[SIZE] . 如何在运行时使其成为可能(当然只有在可能的情况下),例如int (*foo(size_t row, size_t col))[SIZE]

If SIZE is not a compile-time constant then you cannot do that. 如果SIZE不是编译时常量,那么你不能这样做。 C2011 holds that C2011认为

If an identifier is declared as having a variably modified type, it shall [...] have no linkage, and have either block scope or function prototype scope. 如果标识符被声明为具有可变修改类型,则它应该没有链接,并且具有块范围或函数原型范围。 [...] [...]

(6.7.6.2/2) (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). 换句话说,因为所有函数都具有文件范围以及内部或外部链接,所以函数返回类型不能是VLA,指向VLA的指针或任何此类型(这些是“可变修改”类型)。

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. 返回类型不包含有关指向数组长度的信息,但如果C允许函数返回可变修改类型,那么无论如何,这将依赖于代码可以知道任何特定上下文中的变量维度的机制。 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. 如@ChronoKitsune所建议的那样,也可以声明你的函数返回一个指向未指定大小的数组的指针。 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];

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

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