简体   繁体   English

将表示2D数组的指针传递给C ++中的函数

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

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. 我有一个extern“C”类型的函数,我想通过这个双重的东西[3] [3]来。 So I think i need to make it a pointer, right? 所以我想我需要把它作为指针,对吧?

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)传递指向整个2D数组的指针:

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)传递指向第一个1D数组(第一行)和行数的指针

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) (这最后一个选项不是严格符合标准的,因为它将指针推进到1D数组的元素(第一行)超过该数组的末尾)

If that wasn't a C function, there'd be a few more options! 如果那不是C函数,还有更多选项!

Your assigned is flawed. 你指定的是有缺陷的。 p_stuff; is pointer to pointer to double whereas stuff is two dimensional array( array of arrays) 是指向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;

double ** p_stuff; corresponds to an array of pointer to double . 对应于指向double指针数组。 double stuff[3][3] doesn't have any pointers - it's a 2D array of double. double stuff[3][3]没有任何指针 - 它是一个double的2D数组。

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

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)支持运行时大小的数组,因此您无需提前进行转换。

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

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