2012-12-31 56 views
0

Possible Duplicate:
Create a pointer to two-dimensional array2维数组和双指针

当我调用函数FUNC4()和func5()我得到的followinfg错误:

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; 
} 
+0

我有一个建议你:不要写多语言源文件。例如:头文件“”不是C头; 'printf()'的写法通常在C语言中(也许'cout'在C++中更常见)。 – pmg

回答

0

的问题是在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; 
    } 
} 
+0

谢谢你的回答。不改变函数func4()和func5()的解决方案是从main:func4((short **)mat)调用函数; func5((short **)mat); –

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此,如果事情可以通过一种方式容易且干净完成后,不要试图通过肮脏的和困难的方式,因为它会做到这一点在将来修改或更新代码时会让自己感到迷惑。

+0

为清洁和简单的答案+1的好建议 – Naruto

1

1.They是相同的:

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

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

2.Maybe你可以试试这个:

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.我认为第三FUNC是最好的!它使用了一个名为“flattening the array”的技巧,我在指针上读取了C

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.