【问题标题】:2 dim array and double pointer [duplicate]2暗淡数组和双指针[重复]
【发布时间】:2012-12-15 12:47:12
【问题描述】:

可能重复:
Create a pointer to two-dimensional array

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

func4() 错误:无法将“short int (*)[3]”转换为“short int**” 参数‘1’到‘int func4(short int**)’| func5() 错误:不能 将参数“1”的“short int (*)[3]”转换为“short int**”到“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;
}

【问题讨论】:

  • 我给你一个建议:不要写多语言源文件。例如:标头&lt;cstdio&gt;不是C标头; printf() 的写法在 C 中很常见(也许cout 在 C++ 中更常见)。

标签: c++ c arrays pointers multidimensional-array


【解决方案1】:

can't 发送一个二维数组来运行,但未指定数组的 length or size of second dimension。这就是导致错误的原因。

试试这个:

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;

}

Remember 如果某件事可以通过一种方式轻松干净地完成,请不要尝试通过肮脏和困难的方式来完成它,因为它会在您将来修改或更新代码时感到困惑。

【讨论】:

  • +1 简洁明了的答案和不错的建议
【解决方案2】:

1.它们是一样的:

int func(short **mat);
int func(short *mat[]);
int func(short *mat[3]);

short **short (*)[3]不兼容,因为它们指向的类型不同。 short ** 指向short *short (*)[3] 指向short[3]

2.也许你可以试试这个:

int funcMy(void *mat)   // OK! -added by Justme0 2012/12/31
{
    short i, j, *index[3];
    for (i = 0 ; i < 3 ; i++)
        index[i] = (short *)mat + 3*i;

    printf(" Declare as (void *) 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;
}

3.我认为第三个功能是最好的!它使用了我在 POINTERS ON C 中读到的一个名为“扁平化数组”的技巧。

方法#3(使用单个指针,数组被“展平”) ==================================================== ========== 使用此方法,您可以创建通用例程。尺寸不 出现在任何声明中,因此您可以将它们添加到形式参数中 列表。

手动数组索引可能会减慢执行速度。

【讨论】:

    【解决方案3】:

    问题在于func4()func5() 的矩阵定义不正确。

    您将其定义为short mat[3][3],但在这种情况下,您实际上并未分配指向矩阵行的指针数组。你得到一个指向一个连续内存块的指针。

    如果要将参数矩阵传递为short int**,则应将其定义如下:

    #include <stdlib.h>
    
    short int** mat;
    
    for(int i = 0; i < 3; i++) {
        mat[i] = (short int*)malloc (3*sizeof(short int));
        for(int j = 0; j < 3; i++) {
             mat[i][j] = i*10 + j;
        }
    }
    

    【讨论】:

    • 感谢您的回答。不更改函数 func4() 和 func5() 的解决方案是从 main 调用函数: func4((short **)mat); func5((短 **)mat);
    猜你喜欢
    • 2012-11-17
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    相关资源
    最近更新 更多