如何在C中将平面阵列重塑为二维阵列?
我有一个数组,其中x*x元素被定义为指针 我想做这样的事情:如何在C中将平面阵列重塑为二维阵列?,c,C,我有一个数组,其中x*x元素被定义为指针 我想做这样的事情: void func(const unsigned int x, const int *flat_array) { const int (*reshaped)[x] = flat_array; ... } 但是怎么做呢?这应该可以做到: int m,n; typedef int myarray_t[x][x]; myarray_t *myarray; myarray = (myarray_t *) flat_array
void func(const unsigned int x, const int *flat_array)
{
const int (*reshaped)[x] = flat_array;
...
}
但是怎么做呢?这应该可以做到:
int m,n;
typedef int myarray_t[x][x];
myarray_t *myarray;
myarray = (myarray_t *) flat_array;
for (m=0;m<x;m++)
for (n=0;n<x;n++)
printf("[%d][%d] : %d\n",m,n,(*myarray)[m][n]);
intm,n;
typedef int myarray_t[x][x];
myarray_t*myarray;
myarray=(myarray_t*)平面数组;
对于(m=0;m这应该做到:
int m,n;
typedef int myarray_t[x][x];
myarray_t *myarray;
myarray = (myarray_t *) flat_array;
for (m=0;m<x;m++)
for (n=0;n<x;n++)
printf("[%d][%d] : %d\n",m,n,(*myarray)[m][n]);
intm,n;
typedef int myarray_t[x][x];
myarray_t*myarray;
myarray=(myarray_t*)平面数组;
对于(m=0;m如果原始是malloced,我理解重塑的含义
#define MIN(x,y) ((x) > (y) ? (y) : (x))
int *reshape(int *src, int srcRows, int srcCols, int newRows, int newColums)
{
int *newarray = (*src != NULL && newRows > 0 && newColums > 0) ? malloc(newRows * newColums) : NULL;
if (newarray != NULL)
{
for (int row = 0; row < MIN(srcRows, newRows); row++)
for (int col = 0; col < MIN(srcCols, newColums); col++)
*(newarray + row * newColums + col) = *(src + row * srcCols + col);
free(src);
}
return newarray;
}
定义最小值(x,y)((x)>(y)?(y):(x))
int*重塑(int*src、int-srcRows、int-srcCols、int-newRows、int-newColums)
{
int*newarray=(*src!=NULL&&newRows>0&&newcolumns>0)?malloc(newRows*newColums):NULL;
if(newarray!=NULL)
{
对于(int row=0;row
如果新阵列更小,一些数据将丢失。如果原始阵列是malloced,我理解重塑的含义
#define MIN(x,y) ((x) > (y) ? (y) : (x))
int *reshape(int *src, int srcRows, int srcCols, int newRows, int newColums)
{
int *newarray = (*src != NULL && newRows > 0 && newColums > 0) ? malloc(newRows * newColums) : NULL;
if (newarray != NULL)
{
for (int row = 0; row < MIN(srcRows, newRows); row++)
for (int col = 0; col < MIN(srcCols, newColums); col++)
*(newarray + row * newColums + col) = *(src + row * srcCols + col);
free(src);
}
return newarray;
}
定义最小值(x,y)((x)>(y)?(y):(x))
int*重塑(int*src、int-srcRows、int-srcCols、int-newRows、int-newColums)
{
int*newarray=(*src!=NULL&&newRows>0&&newcolumns>0)?malloc(newRows*newColums):NULL;
if(newarray!=NULL)
{
对于(int row=0;row
如果新数组较小,一些数据将丢失。这段代码对我来说很有用。请注意,它使用了C99(和C11)可变长度数组(VLA)。它不会用C89/C90编译器编译
#include <stdio.h>
static void func(const unsigned int x, const int *flat_array)
{
const int (*reshaped)[x] = (void *)flat_array;
for (unsigned int i = 0; i < x; i++)
{
for (unsigned int j = 0; j < x; j++)
printf("%3d", reshaped[i][j]);
putchar('\n');
}
}
int main(void)
{
int flat[] =
{
/* random -n 25 10 99 | commalist -n 5 -b ' ' */
73, 34, 76, 48, 17,
25, 71, 11, 87, 74,
18, 87, 11, 47, 32,
33, 62, 41, 55, 90,
90, 28, 69, 58, 29,
};
for (int i = 0; i < 5; i++)
{
printf("R[%d]", i);
for (int j = 0; j < 5; j++)
printf(" [%d] = %2d", j, flat[i * 5 + j]);
putchar('\n');
}
func(5, flat);
return 0;
}
如果函数中没有强制转换(tovoid*
),则会出现以下错误:
initialization from incompatible pointer type [-Werror=incompatible-pointer-types]
void*
cast是处理此问题的有效方法,但它很容易被误用。您还可以编写:
const int (*reshaped)[x] = (int (*)[x])flat_array;
它直接显式强制转换到正确的类型-指向行宽为x
的整数数组的指针。如果需要,可以在强制转换中添加const
输出:
R[0] [0] = 73 [1] = 34 [2] = 76 [3] = 48 [4] = 17
R[1] [0] = 25 [1] = 71 [2] = 11 [3] = 87 [4] = 74
R[2] [0] = 18 [1] = 87 [2] = 11 [3] = 47 [4] = 32
R[3] [0] = 33 [1] = 62 [2] = 41 [3] = 55 [4] = 90
R[4] [0] = 90 [1] = 28 [2] = 69 [3] = 58 [4] = 29
73 34 76 48 17
25 71 11 87 74
18 87 11 47 32
33 62 41 55 90
90 28 69 58 29
这段代码适合我。请注意,它使用C99(和C11)可变长度数组(VLA)。它不会用C89/C90编译器编译
#include <stdio.h>
static void func(const unsigned int x, const int *flat_array)
{
const int (*reshaped)[x] = (void *)flat_array;
for (unsigned int i = 0; i < x; i++)
{
for (unsigned int j = 0; j < x; j++)
printf("%3d", reshaped[i][j]);
putchar('\n');
}
}
int main(void)
{
int flat[] =
{
/* random -n 25 10 99 | commalist -n 5 -b ' ' */
73, 34, 76, 48, 17,
25, 71, 11, 87, 74,
18, 87, 11, 47, 32,
33, 62, 41, 55, 90,
90, 28, 69, 58, 29,
};
for (int i = 0; i < 5; i++)
{
printf("R[%d]", i);
for (int j = 0; j < 5; j++)
printf(" [%d] = %2d", j, flat[i * 5 + j]);
putchar('\n');
}
func(5, flat);
return 0;
}
如果函数中没有强制转换(tovoid*
),则会出现以下错误:
initialization from incompatible pointer type [-Werror=incompatible-pointer-types]
void*
cast是处理此问题的有效方法,但它很容易被误用。您还可以编写:
const int (*reshaped)[x] = (int (*)[x])flat_array;
它直接显式强制转换到正确的类型-指向行宽为x
的整数数组的指针。如果需要,可以在强制转换中添加const
输出:
R[0] [0] = 73 [1] = 34 [2] = 76 [3] = 48 [4] = 17
R[1] [0] = 25 [1] = 71 [2] = 11 [3] = 87 [4] = 74
R[2] [0] = 18 [1] = 87 [2] = 11 [3] = 47 [4] = 32
R[3] [0] = 33 [1] = 62 [2] = 41 [3] = 55 [4] = 90
R[4] [0] = 90 [1] = 28 [2] = 69 [3] = 58 [4] = 29
73 34 76 48 17
25 71 11 87 74
18 87 11 47 32
33 62 41 55 90
90 28 69 58 29
假设指针指向数组,即相同类型的相邻项块,则可以安全地强制转换指针类型:
const int (*reshaped)[x] = (const int(*)[x])flat_array;
这是可以保证工作的,因为C中的类型别名规则只关注指向数据的性质,而不太关注所涉及的指针类型。(参见有效类型的概念,6.5/6).假设指针确实指向数组,即-相同类型的相邻项块,则可以安全地强制转换指针类型:
const int (*reshaped)[x] = (const int(*)[x])flat_array;
这是可以保证有效的,因为C中的类型别名规则只关注指向数据的性质,而不太关注所涉及的指针类型。(请参阅有效类型的概念,6.5/6)。这意味着什么?@Davidspataro,用[x]乘以[x]将x*x元素的平面数组重塑为2d数组 elementes@PeterJ,重塑:在不更改数组数据的情况下为数组提供新形状(numpy定义)进行强制转换或进行强制转换,您得到的应该可以工作。有什么问题吗?您确实需要C11(或C99)编译器,而不是C89编译器。获得该消息的唯一原因是,您使用的编译器不支持可变长度数组,可变长度数组是C99的一项强制性功能,在C11中成为一项可选功能。这意味着什么?@DavideSpataro,将x*x元素的平面数组重塑为具有[x] 由[x]elementes@PeterJ,重塑:在不更改数组数据的情况下为数组提供新形状(numpy定义)进行强制转换或进行强制转换,您得到的应该可以工作。有什么问题吗?您确实需要C11(或C99)编译器,而不是C89编译器。获取该消息的唯一原因是您使用的编译器不支持可变长度数组,可变长度数组是C99的一项必需功能,在C11中被添加为可选功能。什么是commalist?@DavideSpataro:
commalist
是一个包装awk sc的中等复杂的shell脚本从输入中生成逗号分隔值列表的脚本。目前有165行shell(包括适度丰富的注释),其中大约50行是Awk。选项显示“每行输出打印5个值,每行以8个空格开头”。如果您感兴趣,请呼喊-查看我的个人资料。(随机程序也是我自己自制的。它没有我想要的那么精致,尽管它能生成10到99之间的25个数字,但非常干净。)commalist是什么?@DavideSpataro:commalist
是一个中等复杂的shell脚本,它包装了一个awk脚本,从输入中生成一个逗号分隔值列表。目前有165行shell(包括中等丰富的注释),其中大约50行是awk。选项如下“每行输出打印5个值,每行以8个空格开始”。如果您感兴趣,请大声喊叫-查看我的个人资料。random
程序也是我自己的自制程序。它没有我想要的那么完美,尽管生成10到99之间的25个数字,它非常干净。)