将表示二维数组的指针传递给 C++ 中的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5329107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:55:20  来源:igfitidea点击:

Passing a pointer representing a 2D array to a function in C++

c++arrays

提问by Derek

http://www.neilstuff.com/guide_to_cpp/notes/Multi%20Dimension%20Arrays%20and%20Pointer%20Pointers.htm

http://www.neilstuff.com/guide_to_cpp/notes/Multi%20Dimension%20Arrays%20and%20Pointer%20Pointers.htm

According to this site, I should be able to use the following code:

根据此站点,我应该可以使用以下代码:

double stuff[3][3];
double **p_stuff;
p_stuff = stuff;

But I get a complaint that the conversion is not allowed by assignment.

但是我收到投诉,说分配不允许转换。

Am I doing something wrong?

难道我做错了什么?

I have an extern "C" type function that I want to pass this double stuff[3][3] to. So I think i need to make it a pointer, right?

我有一个 extern "C" 类型的函数,我想将这个 double stuff[3][3] 传递给。所以我想我需要让它成为一个指针,对吗?

回答by Cubbi

Regarding the edit: to pass this double stuff[3][3]to a C function, you could

关于编辑:要将其传递double stuff[3][3]给 C 函数,您可以

1) pass a pointer to the whole 2D array:

1) 传递一个指向整个二维数组的指针:

void dostuff(double (*a)[3][3])
{
// access them as (*a)[0][0] .. (*a)[2][2]
}
int main()
{
    double stuff[3][3];
    double (*p_stuff)[3][3] = &stuff;
    dostuff(p_stuff);
}

2) pass a pointer to the first 1D array (first row) and the number of rows

2)传递指向第一个一维数组(第一行)和行数的指针

void dostuff(double a[][3], int rows)
{
// access them as a[0][0] .. a[2][2]
}
int main()
{
    double stuff[3][3];
    double (*p_stuff)[3] = stuff;
    dostuff(p_stuff, 3);
}

3) pass a pointer to the first value in the first row and the number of both columns and rows

3) 传递指向第一行第一个值的指针以及列数和行数

void dostuff(double a[], int rows, int cols)
{
// access them as a[0] .. a[8];
}
int main()
{
    double stuff[3][3];
    double *p_stuff = stuff[0];
    dostuff(p_stuff, 3, 3);
}

(that this last option is not strictly standards-compliant since it advances a pointer to an element of a 1D array (the first row) past the end of that array)

(这最后一个选项并不严格符合标准,因为它将指向一维数组(第一行)元素的指针提前到该数组的末尾)

If that wasn't a C function, there'd be a few more options!

如果那不是 C 函数,那么还有更多选择!

回答by Prasoon Saurav

Your assigned is flawed. p_stuff;is pointer to pointer to doublewhereas stuffis two dimensional array( array of arrays)

你的分配有缺陷。p_stuff;是指向指针的指针,double而是stuff二维数组(数组数组)

A single dimension array decays to the pointer to its first element. A 2 dimensional array decays to pointer to a single dimension array.

一维数组衰减到指向其第一个元素的指针。二维数组衰减为指向一维数组的指针。

Try this

尝试这个

double stuff[3][3];
double (*p_stuff)[3]; // pointer to array of 3 int
p_stuff = stuff;

回答by Erik

double ** p_stuff;corresponds to an array of pointer to double. double stuff[3][3]doesn't have any pointers - it's a 2D array of double.

double ** p_stuff;对应于指向 double指针数组。double stuff[3][3]没有任何指针 - 它是一个二维数组。

回答by Th?o Hoàng Minh

Below is maybe your answer:

下面也许是你的答案:

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

typedef unsigned int uint;
uint m[10][20];
uint **ppm;

int main() {
    int i;

    ppm = (uint **)m;
    for (i =0; i<10; ++i)ppm[i] = (uint *)&m[i];

    m[1][1] = 10;

    printf("0x%x vs 0x%x: %d vs %d\n", ppm,m, m[1][1], *(*(ppm+1)+1));

    return 0;
}

The result's on console screen:

结果在控制台屏幕上:

0x6010a0 vs 0x6010a0: 10 vs 10

回答by Simon Woo

In addition to Cubbi's detailed answer, the following way is just natural to pass a 2-dim array to a function:

除了 Cubbi 的详细答案之外,以下方式很自然地将 2-dim 数组传递给函数:

void dostuff(void *a1, int rows, int cols)
{
    double (*a)[cols] = (double (*)[cols]) a1;
    // access them as a[0][0] .. a[rows-1][cols-1];
}
int main()
{
    double stuff[3][3];
    dostuff(stuff, 3, 3);
}

You don't have to cast them in advance since the newest C++ (C++14) supports run-time sized arrays.

由于最新的 C++ (C++14) 支持运行时大小的数组,因此您不必提前强制转换它们。