2维数组和双指针 [英] 2 dim array and double pointer
问题描述
可能重复:结果
创建指针二维数组
块引用>当我调用函数FUNC4()和func5()我得到的followinfg错误:
FUNC4()错误:无法转换'短整型()[3]到短整型的*'的
参数'1'到'廉政FUNC4(短整型*的)| func5()错误:不能
转换短整型(的)[3]到短整型*的的论点'1'到'INT
func5(短整型的*)'|
块引用>我怎样才能纠正调用函数FUNC4()和func5()的错误?这里是我的code:
的#include< cstdio>INT func1的(短垫[] [3]);
INT FUNC2(短(*垫)[3]);
INT FUNC3(短*垫);
INT FUNC4(短**垫);
INT func5(短*垫[3]);诠释的main()
{短垫[3] [3],I,J;对于(I = 0; I&下; 3;我+ +)
为(J = 0; J&下; 3; J ++)
{
垫[I] [J] = I * 10 + J;
}的printf(初始化数据:);
对于(I = 0; I&下; 3;我+ +)
{
的printf(\\ n);
为(J = 0; J&下; 3; J ++)
{
的printf(%5.2d垫[I] [J]);
}
}的printf(\\ n);func1的(垫);
FUNC2(垫);
FUNC3(安培;垫[0] [0]);
FUNC4(垫); //错误:无法转换'短整型(*)[3]到
//'短整型**'的说法'1'到'廉政FUNC4(short int类型**)|
func5(垫); //错误:无法转换'短整型(*)[3]到
//'短整型**'的说法'1'到'廉政func5(short int类型**)|返回0;
}/ *
方法1(没有章法,只是空洞的第一维数组)
================================================== =============
您不必指定的第一个维度!
* /INT func1的(短垫[] [3])
{
注册短I,J;的printf(声明为基体,显式地指定第二个维度:);
对于(I = 0; I&下; 3;我+ +)
{
的printf(\\ n);
为(J = 0; J&下; 3; J ++)
{
的printf(%5.2d垫[I] [J]);
}
}
的printf(\\ n);返回0;
}/ *
方法2(指向数组,第二个维度是明确指定)
================================================== ====================
* /INT FUNC2(短(*垫)[3])
{
注册短I,J;的printf(声明为指针列,显式指定第二暗淡:);
对于(I = 0; I&下; 3;我+ +)
{
的printf(\\ n);
为(J = 0; J&下; 3; J ++)
{
的printf(%5.2d垫[I] [J]);
}
}
的printf(\\ n);返回0;
}/ *
方法#3(使用单个指针,阵列扁平化)
================================================== ==========
使用这种方法,您可以创建通用的惯例。
尺寸没有出现在任何声明,所以你
可以将它们添加到正规的参数列表。手动数组索引可能会减慢执行。
* /INT FUNC3(短*垫)
{
注册短I,J;的printf(申报单指针,手动偏移的计算:);
对于(I = 0; I&下; 3;我+ +)
{
的printf(\\ n);
为(J = 0; J&下; 3; J ++)
{
的printf(%5.2d*(垫+ 3 * I + J));
}
}
的printf(\\ n);返回0;
}/ *
方法#4(双指针,使用指针的辅助阵列)
================================================== ==============
使用这种方法,您可以创建通用的惯例,
如果你在运行时分配指数。尺寸添加到正式的参数列表。
* /INT FUNC4(短**垫)
{
总之I,J,*指数[3];对于(I = 0; I&下; 3;我+ +)
指数[i] =(短*)垫+ 3 *我;的printf(标为双指针,使用辅助指针数组:);
对于(I = 0; I&下; 3;我+ +)
{
的printf(\\ n);
为(J = 0; J&下; 3; J ++)
{
的printf(%5.2d,指数[I] [J]);
}
}
的printf(\\ n);返回0;
}/ *
方法#5(单指针,使用指针的辅助阵列)
================================================== ==============
* /INT func5(短*垫[3])
{
总之I,J,*指数[3];
对于(I = 0; I&下; 3;我+ +)
指数[i] =(短*)垫+ 3 *我;的printf(申报单指针,使用辅助指针数组:);
对于(I = 0; I&下; 3;我+ +)
{
的printf(\\ n);
为(J = 0; J&下; 3; J ++)
{
的printf(%5.2d,指数[I] [J]);
}
}
的printf(\\ n);
返回0;
}
解决方案1.They是相同的:
INT FUNC(短**垫);
INT FUNC(短*垫[]);
INT FUNC(短*垫[3]);和
短**
是不是与兼容短(*)[3]
,因为它们指向的类型向是不同的。短**
点短*
,而短(*)[3]
点短[3]
。2.Maybe你可以试试这个:
INT funcMy(无效*垫)// OK!通过Justme0 2012年12月31日 - 新增
{
总之I,J,*指数[3];
对于(I = 0; I&下; 3;我+ +)
指数[i] =(短*)垫+ 3 *我; 的printf(标为(无效*)指针,使用辅助指针数组:);
对于(I = 0; I&下; 3;我+ +)
{
的printf(\\ n);
为(J = 0; J&下; 3; J ++)
{
的printf(%5.2d,指数[I] [J]);
}
}
的printf(\\ n);
返回0;
}3.我认为第三FUNC是最好的!它使用一个名为扁平化数组,我读招的 POINTERS ONç的
方法#3(使用单个指针,阵列扁平化)
================================================== ==========使用这种方法,您可以创建通用的惯例。尺寸不
出现在任何声明,所以你可以将它们添加到正式的说法
名单。
本手册的数组索引可能会减慢执行。
块引用>Possible Duplicate:
Create a pointer to two-dimensional arrayWhen I calling the functions func4() and func5() i get the followinfg errors:
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*)’|
How can I correct the error on calling functions func4() and func5()? Here is my code:
#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; }
解决方案1.They are the same:
int func(short **mat); int func(short *mat[]); int func(short *mat[3]);
And
short **
is not compatible withshort (*)[3]
, because the types they point to are different.short **
points toshort *
whileshort (*)[3]
points toshort[3]
.2.Maybe you can try this:
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.I think the 3rd func is the best! It uses a trick named "flattening the array" that I read in POINTERS ON 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.
这篇关于2维数组和双指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!