C语言中的多维数组数据类型
我试图在函数中创建一个二维数组,然后在其他地方使用它,但是我到处都被不兼容的指针类型所困扰。(我知道,已经讨论过类似的话题,但仍然无法从现有的董事会中清楚地找到答案。) 故事是这样的: 我可以将数组传递给这样的函数(允许使用c99):C语言中的多维数组数据类型,c,arrays,C,Arrays,我试图在函数中创建一个二维数组,然后在其他地方使用它,但是我到处都被不兼容的指针类型所困扰。(我知道,已经讨论过类似的话题,但仍然无法从现有的董事会中清楚地找到答案。) 故事是这样的: 我可以将数组传递给这样的函数(允许使用c99): scanf(“%d”和&arr[i][j])上的编译失败行,由于arr类型不兼容 那么,如何将int*p转换为与int arr[][]相同的类型(以及它的确切类型),以便在程序中的其他地方重用它?或者我为这件事做了不同类型的分配 提前谢谢 这可能对你有帮助 if
scanf(“%d”和&arr[i][j])上的编译失败代码>行,由于arr类型不兼容
那么,如何将int*p
转换为与int arr[][]
相同的类型(以及它的确切类型),以便在程序中的其他地方重用它?或者我为这件事做了不同类型的分配
提前谢谢 这可能对你有帮助
if (( c = ( int** )malloc( N*sizeof( int* ))) == NULL )
{ /* error */ }
for ( i = 0; i < N; i++ )
{
/* x_i here is the size of given row, need to
* multiply by sizeof( int )
*/
if (( c[i] = ( int* )malloc( x_i )) == NULL )
{ /* error */ }
/* probably init the row here */
}
if((c=(int**)malloc(N*sizeof(int*))==NULL)
{/*错误*/}
对于(i=0;i
这可能会对您有所帮助
if (( c = ( int** )malloc( N*sizeof( int* ))) == NULL )
{ /* error */ }
for ( i = 0; i < N; i++ )
{
/* x_i here is the size of given row, need to
* multiply by sizeof( int )
*/
if (( c[i] = ( int* )malloc( x_i )) == NULL )
{ /* error */ }
/* probably init the row here */
}
if((c=(int**)malloc(N*sizeof(int*))==NULL)
{/*错误*/}
对于(i=0;i
这可能会对您有所帮助
if (( c = ( int** )malloc( N*sizeof( int* ))) == NULL )
{ /* error */ }
for ( i = 0; i < N; i++ )
{
/* x_i here is the size of given row, need to
* multiply by sizeof( int )
*/
if (( c[i] = ( int* )malloc( x_i )) == NULL )
{ /* error */ }
/* probably init the row here */
}
if((c=(int**)malloc(N*sizeof(int*))==NULL)
{/*错误*/}
对于(i=0;i
这可能会对您有所帮助
if (( c = ( int** )malloc( N*sizeof( int* ))) == NULL )
{ /* error */ }
for ( i = 0; i < N; i++ )
{
/* x_i here is the size of given row, need to
* multiply by sizeof( int )
*/
if (( c[i] = ( int* )malloc( x_i )) == NULL )
{ /* error */ }
/* probably init the row here */
}
if((c=(int**)malloc(N*sizeof(int*))==NULL)
{/*错误*/}
对于(i=0;i
首先,您会遇到一个错误:必须将void*类型从malloc显式转换为int*:
int *arr= (int*) malloc((*size_x)*(*size_y)*sizeof(int));
其次,如果只分配一块RAM,则必须自己计算数组索引。例如:
arr[y*(*size_x)+x] = 123;
因此,您必须为(*size_x)个int*
指针分配一个数组,然后您必须分配(*size_x)个单独的int
数组,每个数组的大小为(*size_y),并将它们的指针存储到int*
数组中。然后您可以使用arr[x][y]
符号。首先您会遇到一个错误:您必须显式地将void*类型从malloc转换为int*:
int *arr= (int*) malloc((*size_x)*(*size_y)*sizeof(int));
其次,如果只分配一块RAM,则必须自己计算数组索引。例如:
arr[y*(*size_x)+x] = 123;
因此,您必须为(*size_x)个int*
指针分配一个数组,然后您必须分配(*size_x)个单独的int
数组,每个数组的大小为(*size_y),并将它们的指针存储到int*
数组中。然后您可以使用arr[x][y]
符号。首先您会遇到一个错误:您必须显式地将void*类型从malloc转换为int*:
int *arr= (int*) malloc((*size_x)*(*size_y)*sizeof(int));
其次,如果只分配一块RAM,则必须自己计算数组索引。例如:
arr[y*(*size_x)+x] = 123;
因此,您必须为(*size_x)个int*
指针分配一个数组,然后您必须分配(*size_x)个单独的int
数组,每个数组的大小为(*size_y),并将它们的指针存储到int*
数组中。然后您可以使用arr[x][y]
符号。首先您会遇到一个错误:您必须显式地将void*类型从malloc转换为int*:
int *arr= (int*) malloc((*size_x)*(*size_y)*sizeof(int));
其次,如果只分配一块RAM,则必须自己计算数组索引。例如:
arr[y*(*size_x)+x] = 123;
因此,您必须为(*size_x)个int*
指针分配一个数组,然后您必须分配(*size_x)个单独的int
数组,每个数组的大小为(*size_y),并将它们的指针存储到int*
数组中。然后您可以使用arr[x][y]
符号。我认为您需要一个指针数组,每个指针分配一个整数数组
int** makeTable(int size_x, int size_y){
int z;
int** table;
/* get an array of pointers of length x */
table = malloc(sizeof(int*) * size_x);
for(z = 0; z < size_x; z++){
/* call calloc to zero out an array */
table[z] = calloc(sizeof(int), size_y);
}
return table;
}
int**makeTable(int size\u x,int size\u y){
intz;
int**表格;
/*获取长度为x的指针数组*/
表=malloc(sizeof(int*)*size_x);
对于(z=0;z
然后您可以使用表[i][j]
我想您需要一个指针数组,每个指针都分配了一个整数数组
int** makeTable(int size_x, int size_y){
int z;
int** table;
/* get an array of pointers of length x */
table = malloc(sizeof(int*) * size_x);
for(z = 0; z < size_x; z++){
/* call calloc to zero out an array */
table[z] = calloc(sizeof(int), size_y);
}
return table;
}
int**makeTable(int size\u x,int size\u y){
intz;
int**表格;
/*获取长度为x的指针数组*/
表=malloc(sizeof(int*)*size_x);
对于(z=0;z
然后您可以使用表[i][j]
我想您需要一个指针数组,每个指针都分配了一个整数数组
int** makeTable(int size_x, int size_y){
int z;
int** table;
/* get an array of pointers of length x */
table = malloc(sizeof(int*) * size_x);
for(z = 0; z < size_x; z++){
/* call calloc to zero out an array */
table[z] = calloc(sizeof(int), size_y);
}
return table;
}
int**makeTable(int size\u x,int size\u y){
intz;
int**表格;
/*获取长度为x的指针数组*/
表=malloc(sizeof(int*)*size_x);
对于(z=0;z
然后您可以使用表[i][j]
我想您需要一个指针数组,每个指针都分配了一个整数数组
int** makeTable(int size_x, int size_y){
int z;
int** table;
/* get an array of pointers of length x */
table = malloc(sizeof(int*) * size_x);
for(z = 0; z < size_x; z++){
/* call calloc to zero out an array */
table[z] = calloc(sizeof(int), size_y);
}
return table;
}
int**makeTable(int size\u x,int size\u y){
intz;
int**表格;
/*获取长度为x的指针数组*/
表=malloc(sizeof(int*)*size_x);
对于(z=0;z