简体   繁体   English

返回多维数组的指针

[英]Returning a pointer of a multidimensional array

I'm Java programmer and I'm struggling with this simple stuff. 我是Java程序员,我正在努力解决这些简单的问题。

How can I return this multidimensional array? 我怎样才能返回这个多维数组? Does it have to return a ** pointer? 是否必须返回**指针? How can I get it in another file? 我如何在另一个文件中获取它?

static MoveDirection ghost_moves[GHOSTS_SIZE][4];

MoveDirection** get_ghost_moves() {
    return ghost_moves;
}

A 2D array is not a pointer to a pointer. 2D数组不是指向指针的指针。 Arrays and pointers are fundamentally different types in C and C++. 数组和指针在C和C ++中基本上是不同的类型。 An array decays into a pointer to its first element (hence the frequent confusion between the two), but it only decays at the first level: if you have a multidimensional array, it decays into a pointer to an array, not a pointer to a pointer. 数组衰变为指向其第一个元素的指针(因此两者之间经常混淆),但它只在第一级衰减:如果你有一个多维数组,它会衰变为指向数组的指针,而不是指向数组的指针指针。

The proper declaration of get_ghost_moves is to declare it as returning a pointer to an array: get_ghost_moves的正确声明是将其声明为返回指向数组的指针:

static MoveDirection ghost_moves[GHOSTS_SIZE][4];

// Define get_ghost_moves to be a function returning "pointer to array 4 of MoveDirection"
MoveDirection (*get_ghost_moves())[4] {
    return ghost_moves;
}

This syntax syntax is extremely confusing, so I recommend making a typedef for it: 这种语法语法非常混乱,所以我建议为它创建一个typedef:

// Define MoveDirectionArray4 to be an alias for "array of 4 MoveDirections"
typedef MoveDirection MoveDirectionArray4[4];

MoveDirectionArray4 *get_ghost_moves() {
    return ghost_moves;
}
#define MAX 20

char canopy[20][MAX];
typedef char frank[MAX];

frank friend[MAX]; 

frank *getList()
{
    return friend;
}

void test(int n)
{
    frank *list = getList();

    int i;

    for (i = 0; i < 5; i++ )
    {       
        printf("list[%d] %s\n\r", i, list[i]);
    } 
}

int main(void)
{
    int i, nf;

    for (i = 0; i < 5; i++ )
    {
        snprintf(friend[i], MAX, "test%d", i);
        printf("test[%d] %s \n\r", i, friend[i]);
    } 

    test(5);

    return 0;
}

I believe that you want the following: 我相信你想要以下内容:

MoveDirection ( * get_ghost_moves() ) [GHOSTS_SIZE][4];

In the above get_ghost_moves is a function returning a pointer to an array of array of MoveDirection , with dimensions GHOSTS_SIZE and 4. 在上面, get_ghost_moves是一个返回指向MoveDirection数组数组的指针的函数,其尺寸为GHOSTS_SIZE和4。

I found the following two sites very useful for learning how to understand C declarations: 我发现以下两个站点对于学习如何理解C声明非常有用:

No, just a single * is all you need. 不,只需要一个*即可。 The key is to think about how your data is arranged in memory: an array is just a series of items of the same type, laid out contiguously in memory. 关键是要考虑数据在内存中的排列方式:数组只是一系列相同类型的项目,在内存中连续排列。 In this context it doesn't matter what shape or size the array is - you're just returning a pointer to the start of it, which means you're returning the address of the first item. 在这种情况下,数组的形状或大小并不重要 - 您只是返回一个指向它开头的指针,这意味着您将返回第一个项目的地址。

Of course, whoever you're returning that pointer to will have to know what the shape and size of the array are. 当然,无论你返回指针的是谁,都必须知道数组的形状和大小。

One of the nice things about C and pointers is that everything's just a number, and memory is just a big array of bytes. 关于C和指针的一个好处是,一切都只是一个数字,而内存只是一个很大的字节数组。 Once you get comfortable thinking like that, it all falls into place. 一旦你这样思考就会感到舒服,那一切都会落到实处。

ghost_moves occupies a contiguous set of GHOSTS_SIZE*4 MoveDirections in memory: its equivalent to a MoveDirection* ghost_moves在内存中占用一组连续的GHOSTS_SIZE*4 MoveDirections:它相当于一个MoveDirection*

It cannot be used with a pointer to pointer, but with a single pointer. 它不能与指针指针一起使用,而是使用单个指针。


If you want to use two operator[] -s to index your array you've got two options: 如果你想使用两个operator[] -s索引你的数组,你有两个选择:

  1. use a variable of type: MoveDirection (*ghost_moves) [GHOST_SIZE] and then just do ghost_moves[i][j] 使用类型的变量: MoveDirection (*ghost_moves) [GHOST_SIZE]然后只做ghost_moves[i][j]

  2. this is mostly to have a pointer to pointer, usually is not a good solution: have ghost_moves of type MoveDirection** 这主要是指向指针,通常不是一个好的解决方案:拥有ghost_moves类型的MoveDirection**

    for example: 例如:

     MoveDirection ghost_moves_original[GHOST_SIZE][4]; MoveDirection *ghost_moves[GHOST_SIZE]; ghost_moves[0] = ghost_moves_original; ghost_moves[1] = ghost_moves_original + GHOST_SIZE; ... 

A multidimensional array is not an array of pointers. 多维数组不是指针数组。 It is a single memory block. 它是一个单独的内存块。 It only has meaning inside the scope where it is declared. 它只在声明范围内有意义。

You can't cleanly return that as you have here.. you would have to return a MoveDirection[GHOSTS_SIZE][4] . 你不能像在这里那样干净利落地返回..你必须返回一个MoveDirection[GHOSTS_SIZE][4]

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

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