c++ - 2 昏暗数组和双指针

标签 c++ c arrays pointers multidimensional-array

<分区>

Possible Duplicate:
Create a pointer to two-dimensional array

当我调用函数 func4() 和 func5() 时,出现以下错误:

func4() error: cannot convert ‘short int (*)[3]’ to ‘short int**’ for argument ‘1’ to ‘int func4(short int**)’| func5() error: cannot convert ‘short int (*)[3]’ to ‘short int**’ for argument ‘1’ to ‘int func5(short int**)’|

如何更正调用函数 func4() 和 func5() 时的错误?这是我的代码:

#include <cstdio>

int func1(short mat[][3]);
int func2(short (*mat)[3]);
int func3(short *mat);
int func4(short **mat);
int func5(short *mat[3]);

int main()
{

short mat[3][3],i,j;

for(i = 0 ; i < 3 ; i++)
    for(j = 0 ; j < 3 ; j++)
    {
        mat[i][j] = i*10 + j;
    }

printf(" Initialized data to: ");
for(i = 0 ; i < 3 ; i++)
{
    printf("\n");
    for(j = 0 ; j < 3 ; j++)
    {
        printf("%5.2d", mat[i][j]);
    }
}

printf("\n");

func1(mat);
func2(mat);
func3(&mat[0][0]);
func4(mat); //error: cannot convert ‘short int (*)[3]’ to 
            //‘short int**’ for argument ‘1’ to       ‘int func4(short int**)’|
func5(mat); //error: cannot convert ‘short int (*)[3]’ to 
            //‘short int**’ for argument ‘1’ to ‘int func5(short int**)’|

return 0;
}



/*
Method #1 (No tricks, just an array with empty first dimension)
===============================================================
You don't have to specify the first dimension!
*/

int func1(short mat[][3])
{
register short i, j;

printf(" Declare as matrix, explicitly specify second dimension: ");
for(i = 0 ; i < 3 ; i++)
{
    printf("\n");
    for(j = 0 ; j < 3 ; j++)
    {
        printf("%5.2d", mat[i][j]);
    }
}
printf("\n");

return 0;
}

/*
Method #2 (pointer to array, second dimension is explicitly specified)
======================================================================
*/

int func2(short (*mat)[3])
{
register short i, j;

printf(" Declare as pointer to column, explicitly specify 2nd dim: ");
for(i = 0 ; i < 3 ; i++)
{
    printf("\n");
    for(j = 0 ; j < 3 ; j++)
    {
        printf("%5.2d", mat[i][j]);
    }
}
printf("\n");

return 0;
}

/*
Method #3 (Using a single pointer, the array is "flattened")
============================================================
With this method you can create general-purpose routines.
The dimensions doesn't appear in any declaration, so you
can add them to the formal argument list.

The manual array indexing will probably slow down execution.
*/

int func3(short *mat)
{
register short i, j;

printf(" Declare as single-pointer, manual offset computation: ");
for(i = 0 ; i < 3 ; i++)
{
    printf("\n");
    for(j = 0 ; j < 3 ; j++)
    {
        printf("%5.2d", *(mat + 3*i + j));
    }
}
printf("\n");

return 0;
}

/*
Method #4 (double pointer, using an auxiliary array of pointers)
================================================================
With this method you can create general-purpose routines,
if you allocate "index" at run-time.

Add the dimensions to the formal argument list.
*/

int func4(short **mat)
{
short    i, j, *index[3];

for (i = 0 ; i < 3 ; i++)
    index[i] = (short *)mat + 3*i;

printf(" Declare as double-pointer, use auxiliary pointer array: ");
for(i = 0 ; i < 3 ; i++)
{
    printf("\n");
    for(j = 0 ; j < 3 ; j++)
    {
        printf("%5.2d", index[i][j]);
    }
}
printf("\n");

return 0;
}

/*
Method #5 (single pointer, using an auxiliary array of pointers)
================================================================
*/

int func5(short *mat[3])
{
short i, j, *index[3];
for (i = 0 ; i < 3 ; i++)
    index[i] = (short *)mat + 3*i;

printf(" Declare as single-pointer, use auxiliary pointer array: ");
for(i = 0 ; i < 3 ; i++)
{
    printf("\n");
    for(j = 0 ; j < 3 ; j++)
    {
        printf("%5.2d", index[i][j]);
    }
}
printf("\n");
return 0;
}

最佳答案

如果不指定数组的第二维长度或大小,您不能发送一个二维数组来运行。这就是导致错误的原因。

试试这个:

int func4(short mat[][3])
{
short    i, j, *index[3];

for (i = 0 ; i < 3 ; i++)
index[i] = (short *)mat + 3*i;

printf(" Declare as double-pointer, use auxiliary pointer array: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
    printf("%5.2d", index[i][j]);
}
}
printf("\n");

return 0;
}


int func5(short mat[][3])
{
short i, j, *index[3];
for (i = 0 ; i < 3 ; i++)
index[i] = (short *)mat + 3*i;

printf(" Declare as single-pointer, use auxiliary pointer array: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
    printf("%5.2d", index[i][j]);
}
}
printf("\n");
return 0;

记住如果某件事可以通过一种方式轻松干净地完成,不要试图通过一种肮脏和困难的方式来完成,因为它会在您修改或更新您的 future 的代码。

关于c++ - 2 昏暗数组和双指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14100240/

相关文章:

C 中使用 const void * 参数调用函数

我们可以在c语言中创建一个不定义宏#define吗

php - 如何添加到数据库中的数组并取回每个数组并显示它

arrays - 在 PowerShell 中从对象中选择某些属性

c++ - 指针数组分配的段错误?

c++ - 通过 C++ 中的包装器将参数列表传递给构造函数

C 结构指针 - 如何从模块返回结构指针?

arrays - 帮助理解斐波那契搜索

C++ 深度复制对象

c++ - 用 opencv 打开我的笔记本电脑摄像头